Solved

Parse amount field using regular expression

Posted on 2006-11-23
2
2,738 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 63
difference of if loops 23 50
how to see all occupied ports on windows 10 laptop 15 66
Desingning Refactoring existing code 2 24
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

832 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