Thanks, that looks good. I will try it out.
Main Topics
Browse All TopicsI 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})?)(
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!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: StillUnAwarePosted on 2006-11-24 at 02:59:35ID: 18006791
this should work as long it is OK to use BigDecimal, because only this type of object can hold 30 meaningfull digits
0}(\\.\\d{ 0,10})?)([ kKmM])?$") ; Case("k")) Case("m")) alid number");
also import
java.math.BigDecimal;
java.util.regex.*;
public BigDecimal mFunc(String sVal) {
Pattern p = Pattern.compile("^(\\d{1,2
Matcher m = p.matcher(sVal);
if(m.matches()) {
BigDecimal num = new BigDecimal(m.group(1));
if(m.group(3) != null) {
if(m.group(3).equalsIgnore
num = num.multiply(new BigDecimal("1000"));
else if(m.group(3).equalsIgnore
num = num.multiply(new BigDecimal("1000000"));
}
return num;
} else
throw new NumberFormatException("Inv
}