?
Solved

Parse amount field using regular expression

Posted on 2006-11-23
2
Medium Priority
?
2,783 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

777 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