NumberFormat vs double and long

S

StephaneLeFou

Hi,

I have a function which receives an amount (money) in fthe form of a
String. The values received can be of various formats such as:

"99,999.99"
"99 999.99"
"99 999 99"
"99999999"

and so on, depending on the user's input, because the input has not
been formatted yet with the Struts Validator for example...

However, the maximum valid number is: 99999.99d

I'm trying to figure out a way of checking if the passed String (once
parsed and so on) is no bigger than 99999.99d

So far I've been unsuccessful with the parse() and format() with
NumberFormat. Sometimes, it converts it to a double, sometimes to a
long...

Any help would be greatly appreciated.
Thanks.
 
S

Stefan Ram

StephaneLeFou said:
I'm trying to figure out a way of checking if the passed String (once
parsed and so on) is no bigger than 99999.99d

A specification of the syntax and semantics of the
input language used for the numerals is required.
»Various formats such as ...« is not such a specification.
 
S

StephaneLeFou

A specification of the syntax and semantics of the
input language used for the numerals is required.
»Various formats such as ...« is not such a specification.

Oh, I meant: any unsigned number passed in a string that could fill a
mask like 9(9). The MaxLenght of the string is 9 characters and may
or may not include: commas, spaces and decimals.

Thanks again.
 
C

cherryx

Assuming your string is in a variable called a:

//remove anything that isn't a number or a dot
a = a.replaceAll("[^0-9\\.]", "");

//Convert from String to a number variable
BigDecimal b = new BigDecimal(a);

if (b.compareTo(new BigDecimal("99999.99"))==1)
{
System.out.println("Number is bigger than 99999.99");

}



Regards
Chinthaka Weerasinghe
[YOUR FRIENDLY JAVA TUTOR]
 
S

StephaneLeFou

Assuming your string is in a variable called a:

//remove anything that isn't a number or a dot
a = a.replaceAll("[^0-9\\.]", "");

//Convert from String to a number variable
BigDecimal b = new BigDecimal(a);

if (b.compareTo(new BigDecimal("99999.99"))==1)
{
System.out.println("Number is bigger than 99999.99");

}

Regards
Chinthaka Weerasinghe
[YOUR FRIENDLY JAVA TUTOR]
I have a function which receives an amount (money) in fthe form of a
String. The values received can be of various formats such as:
"99,999.99"
"99 999.99"
"99 999 99"
"99999999"
and so on, depending on the user's input, because the input has not
been formatted yet with the Struts Validator for example...
However, the maximum valid number is: 99999.99d
I'm trying to figure out a way of checking if the passed String (once
parsed and so on) is no bigger than 99999.99d
So far I've been unsuccessful with the parse() and format() with
NumberFormat. Sometimes, it converts it to a double, sometimes to a
long...
Any help would be greatly appreciated.
Thanks.

Works like a charm and couldn't be much more simpler than that.

Thank you very much, teacher :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top