Solved

Parse amount field using regular expression

Posted on 2006-11-23
2
2,757 Views
Last Modified: 2010-10-05
I have an HTML input field for entering an amount. It must be 1 to 20 digits, optionally followed by up to 10 decimal digits, and optionally ending with "k" or "m" to denote the thousand or million respectively. So "123.12k" would be parsed as 123120. The following is a JavaScript solution previously provided by Tim Yates:

function mFunc( sVal )
{
    var elems = sVal.match( /^(\d{1,20}(\.\d{0,10})?)([k|m])?$/ ) ;
    if( elems )
    {
        var num = elems[ 1 ] ;
        var mult = elems[ 3 ] ;
        if( mult == 'k' )
        {
            num *= 1000 ;
        }
        else if( mult == 'm' )
        {
            num *= 1000000 ;
        }
        alert( 'value is ' + num ) ;
    }
    else
    {
        alert( 'invalid number' ) ;
    }
}

Now I need the equivalent function in Java. Thanks!
0
Comment
Question by:yongsing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
StillUnAware earned 50 total points
ID: 18006791
this should work as long it is OK to use BigDecimal, because only this type of object can hold 30 meaningfull digits

also import
  java.math.BigDecimal;
  java.util.regex.*;

  public BigDecimal mFunc(String sVal) {
    Pattern p = Pattern.compile("^(\\d{1,20}(\\.\\d{0,10})?)([kKmM])?$");
    Matcher m = p.matcher(sVal);
    if(m.matches()) {
      BigDecimal num = new BigDecimal(m.group(1));
      if(m.group(3) != null) {
        if(m.group(3).equalsIgnoreCase("k"))
          num = num.multiply(new BigDecimal("1000"));
        else if(m.group(3).equalsIgnoreCase("m"))
          num = num.multiply(new BigDecimal("1000000"));
      }
      return num;
    } else
      throw new NumberFormatException("Invalid number");
  }

 
0
 

Expert Comment

by:integrosys
ID: 18006844
Thanks, that looks good. I will try it out.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question