NumberFormat parameter for parsing a currency value

Why the following syntax parsings when parsing using Currency instances in 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();}; 

Above result

 ----------- 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 seems that parsing numbers is good, but it is suffocating when it comes to two copies of a currency, namely:

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

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

Thanks in advance.

+4
source share
1 answer

Because when you parse a currency, the line must also contain a currency symbol, for example, $ for the US locale and Rs. for indian locale.

For example, in Indian local, this is well understood: nf.parse("Rs.1234.56732") , but this fails nf.parse("1234.56732 "). Thus, in your example, you need to add characters with the Franc symbol for successful parsing.

Also, I don’t see a direct way to determine whether the DecimalFormat object is an instance of currency, since the field used for this isCurrencyInstance is declared private in the DecimalFormat class and there is no getter for it.

+6
source

Source: https://habr.com/ru/post/1386932/


All Articles