Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

validating a format xxx.xxxx

hi guys

I have a requirment where i have a decimal field 'rate'  and rate
can be  in format xxx or xxx.xxxx  

for example  rate can be 123 or 123.0000  or 123.3333. These are all valid rates

any idea how i can validate the rate in my java class

String rate = null;
if(rate is not in format xxx or xxx.xxxx)
{

//throw error

}


any idea, how i do this?

thanks
J
Avatar of Ajay-Singh
Ajay-Singh

You can use this,
 
Number x = new DecimalFormat("###.###").parse("122.222");
Avatar of jaggernat

ASKER

ok,thanks but i dint completely understand

>>>Number x = new DecimalFormat("###.###").parse("122.222");

this is what i have so far

String rate = "";
if (rate != "" && rate.length() > 0)

{
how do i use the above code here (Number x = new DecimalFormat("###.###").parse("122.222");)
}


thanks

String rate = "122.222";
try {
  Number x = new DecimalFormat("###.###").parse(rate);
} catch (ParseException ex) {
  ex.printStackTrace();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(My comment takes your comment literally)
thanks all
You might as well use Double.parseDouble, since those NumberFormat let most things through. Try

3.splat

for instance ;-)
hello CEHJ

could you please explain this part of the regex?
(?: .... )?

thank you.

manuel
a. Don't save it
b. Allow one or zero occurences of it

(I assume you know what 'it' is - if not, let me know)
Yes, I know what 'it' is. Thank you for your response and your explanation!