DecimalFormat and NumberFormatException
Hi all,
I have the following code snippet in my application.
DecimalFormat df = new DecimalFormat(",##0.00");
mapNew.put(oKeys, new Double(df.format(d)));
I am getting a NumberFormatException when the value of the variable 'd' is >= 1000.00
How to solve this problem.
Please help
Thanks
Neelambary
# 1
The format() method is used to obtain a String representation of the number in question. You are providing it as an argument to the Double object constr and therein lies the problem.
For any value greater than 999.99, the format method returns a number that has a comma after the 'thousandth' unit. For example 1000 after formatting by your DecimalFormat object would be 1,000.00 which when provided as an arg to the Double class throws a NFE (you have to provide 1000 and not 1,000)
So why do you format your double in first place?
Now if you had a String and wish to convert to a double object with a specified method, you should have used the parse() method instead.
ram.