I have a simple EditTextone that allows the user to enter a number, for example 45.60(example for the American dollar). Then I format this number using the following method:
public String format() {
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.getDefault());
return formatter.format(amount.doubleValue());
}
And on my Android phone, the language is set to English (USA) - therefore, it Locale.getDefault()should return the US locale (and this is so).
Now the editing text has been correctly updated to: $45.60(from here the formatting of the entered number works).
However, if I try to parse the above line "$45.60"using the following method:
NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
Number result = numberFormat.parse("$45.60");
Failure:
java.lang.IllegalArgumentException: Failed to parse amount $45.60 using locale en_US.
/, "45.60" "£45.60" ( ), "£45.60" , .
, (), "45,60" "45,60€" , "45,60€" !
, : , .
- , , ? - ?
unit test, :
public void testCreateStringBased() throws Exception {
CurrencyAmount amount = new CurrencyAmount("25,46€", Locale.GERMANY);
assertEquals(25.46, amount.getAsDouble());
amount = new CurrencyAmount("25,46€", Locale.FRANCE);
assertEquals(25.46, amount.getAsDouble());
amount = new CurrencyAmount("$25.46", Locale.US);
assertEquals(25.46, amount.getAsDouble());
amount = new CurrencyAmount("£25.46", Locale.UK);
assertEquals(25.46, amount.getAsDouble());
}
CurrencyAmount , , , . , .