Error NumberFormat.parse () for some currency lines

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 {

    // For German locale
    CurrencyAmount amount = new CurrencyAmount("25,46€", Locale.GERMANY);
    assertEquals(25.46, amount.getAsDouble());

    // For French locale
    amount = new CurrencyAmount("25,46€", Locale.FRANCE);
    assertEquals(25.46, amount.getAsDouble());

    // For US locale
    amount = new CurrencyAmount("$25.46", Locale.US);
    assertEquals(25.46, amount.getAsDouble());

    // For UK locale
    amount = new CurrencyAmount("£25.46", Locale.UK);
    assertEquals(25.46, amount.getAsDouble());
}

CurrencyAmount , , , . , .

+5
5

, , , :

String value = "$24,76" 
value = value.replace(getCurrencySymbol(locale), StringUtils.EMPTY);

NumberFormat numberFormat = NumberFormat.getInstance(locale);
Number result = numberFormat.parse(value);

, String ... , , , : 45.78 45.78 $45.78 45.78 €....

, , . unittests (. OP) .

- - , , .

+2

:

NumberFormat numberFormat = new DecimalFormat("¤#.00", new DecimalFormatSymbols(Locale.UK));
numberFormat.parse("£123.5678");

¤ - , .

, http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

+2

NumberFormat.getCurrencyInstance(). parse() NumberFormat.getInstance(). parse().

+1

, , , . GBP , NumberFormat en_GB; , "" .

, "12.000"? en-us ; -, - .

Always use NumberFormat.getCurrencyInstance (java.util.Locale) to analyze currency amounts.

0
source

I use below adapted from https://dzone.com/articles/currency-format-validation-and

import java.math.BigDecimal;
import org.apache.commons.validator.routines.*;

BigDecimalValidator currencyValidator = CurrencyValidator.getInstance();
BigDecimal parsedCurrency = currencyValidator.validate(currencyString);
if ( parsedCurrency == null ) {
        throw new Exception("Invalid currency format (please also ensure it is UTF-8)");
}

If you need to insure the correct language used for each user, look at Change language at login

0
source

All Articles