Reputation:
I've this code for INR
long tenderedAmt = (long) NumberFormat.getNumberInstance(new Locale("en", "in")).parse(jAmtTenderedFormattedTextField.getText());`
But when I enter amount 00.50 that throwing exception
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long
Using Double
Double tenderedAmt = (Double) NumberFormat.getNumberInstance(new Locale("en", "in")).parse(jAmtTenderedFormattedTextField.getText());
This snippet throws same exception.
Using
Double tenderedAmt = (Double) jAmtTenderedFormattedTextField.getText());
In above snippet if I've entered 1000.00 it suppose 1.00 instead thousand.
And throwing
java.lang.NumberFormatException: For input string: "1,000.00"
So how can I have approach currency in correct way?
Upvotes: 0
Views: 424
Reputation: 30829
Here's the javadoc of parse
method and this is what it says:
Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.
So, if we assign the result to Number
type, it should give us the correct value, e.g.:
Number result = NumberFormat.getNumberInstance(new Locale("en", "in")).parse("1000.00");
System.out.println(result.doubleValue());
This should print 1000.0
as expected. Also, we don't need to cast
this into any type. We can simply call doubleValue()
or longValue()
or any other xxxValue()
to get the corresponding type.
If it prints 1.0
then I would recommend checking the value returned by jAmtTenderedFormattedTextField.getText()
.
Upvotes: 2