Solved

Parse amount field using regular expression

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
noX challenge 17 76
computer science syllabus 3 52
Which is the latest version of Apache Tomcatl 3 35
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now