ziggy
ziggy

Reputation: 15876

NumberFormat parsing currency value failure

Why is the following generating parse exceptions when parsing using the Currency instances of Number format.

    float f1    =   123.45678f;
    Locale  locFR   =   new Locale("fr");
    NumberFormat[] nfa  =   new NumberFormat[4];

    nfa[0]  =   NumberFormat.getInstance(); //default
    nfa[1]  =   NumberFormat.getInstance(locFR);    //FranceLocale
    nfa[2]  =   NumberFormat.getCurrencyInstance(); //Default Currency
    nfa[3]  =   NumberFormat.getCurrencyInstance(locFR); //French Currency

    for (NumberFormat nf : nfa){

        System.out.println("----------- " +  nf.getClass().getName() + "-----------------------");
        System.out.println("Default maximum fraction digits : " + nf.getMaximumFractionDigits());
        System.out.println(nf.format(f1));

        nf.setMaximumFractionDigits(5);
        System.out.println("Maximum fraction digits updated to : " + nf.getMaximumFractionDigits());
        System.out.println(nf.format(f1));

        try{
            nf.setMaximumFractionDigits(5);
            System.out.println("parsed string: " + nf.parse("1234.56732"));
            nf.setParseIntegerOnly(true);
            System.out.println("after setParseIntegerOnly" + nf.parse("1234.567"));
        }catch (Exception e){e.printStackTrace();}; 

The output of the above is

----------- java.text.DecimalFormat-----------------------
Default maximum fraction digits : 3
123.457
Maximum fraction digits updated to : 5
123.45678
parsed string: 1234.56732
after setParseIntegerOnly1234
----------- java.text.DecimalFormat-----------------------
Default maximum fraction digits : 3
123,457
Maximum fraction digits updated to : 5
123,45678
parsed string: 1234
after setParseIntegerOnly1234
----------- java.text.DecimalFormat-----------------------
Default maximum fraction digits : 2
ú123.46
Maximum fraction digits updated to : 5
ú123.45678
java.text.ParseException: Unparseable number: "1234.56732"
    at java.text.NumberFormat.parse(Unknown Source)
    at TwoMinuteDrillTests.doExamples(TwoMinuteDrillTests.java:859)
    at TwoMinuteDrillTests.main(TwoMinuteDrillTests.java:871)
----------- java.text.DecimalFormat-----------------------
Default maximum fraction digits : 2
123,46 ñ
Maximum fraction digits updated to : 5
123,45678 ñ
java.text.ParseException: Unparseable number: "1234.56732"
    at java.text.NumberFormat.parse(Unknown Source)
    at TwoMinuteDrillTests.doExamples(TwoMinuteDrillTests.java:859)
    at TwoMinuteDrillTests.main(TwoMinuteDrillTests.java:871)

It looks like parsing of numbers is fine but it is choking when it comes to use the two currency instances namely:

    nfa[2]  =   NumberFormat.getCurrencyInstance(); //Default Currency
    nfa[3]  =   NumberFormat.getCurrencyInstance(locFR); //French Currency

Also is there a way i can find out what is the type of the object being processed inside the for loop? I used nf.getClass().getName() in the above example but it always returns the generic type DecimalFormat. I want to be able to know if it is processing a currency instance or a number instance.

Thanks in advance.

Upvotes: 5

Views: 8439

Answers (1)

Suraj Chandran
Suraj Chandran

Reputation: 24791

Because when you are parsing currency the string should contain the currency symbol too, for e.g $ for US locale and Rs. for Indian locale.

For e.g. in Indian local this parses fine: nf.parse("Rs.1234.56732") but this fails nf.parse("1234.56732"). So basically, in your example you need to append the symbols with the Franc symbol for successful parsing.

Also I can't see any direct way of determining whether the DecimalFormat object is a currency instance since the field used for this isCurrencyInstance is declared private in DecimalFormat class and there is no getter for it.

Upvotes: 6

Related Questions