Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Parse amount field using regular expression

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

Technology Partners: 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

618 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