string formatting - Java NumberFormat isn't respecting comma as decimal separator -
I found a bug in my app where when using an international format my currency string is not showing the right group and decimal separator was. I tried to isolate the code and display the problem. In the code below, I am a U.S. I'm making a format and a world format. Both of them use the same currency pattern, but the United States formatter uses a period as the decimal separator and the world format uses a comma as the decimal separator.
When I ask the World Formator to ask what their separators are, then they report them correctly. However, when I get a string for my value, it does not do it properly. This surprisingly uses the duration for grouping and decimal separator.
Public static zero master throws exception (strings [] args {String currencyFormat = "¤¤ #, ## 0.00; - ¤ #, ## 0.00"; Number formatting msaa = number format.getcurrencyinstance (); Decimal functional decimal time currency = AA = (decimal format) currencyUSA; DecimalCurrencyUSA.applyPattern (currencyFormat); Number format currencyworld = number format.getcurrencyinstance (); Decimal fraction decimal decimal currency world = (decimal format) currencyworld; DecimalCurrencyWorld.applyPattern (currencyFormat); Decimal formattus slugs decimal current current symbo = decimal program word.getdiskimmformatsmolls (); / / Decimal decimal phrase set decimal format symbols = new decimal face symbols (Locale.GERMAN); System.out.println (String.format ("Basic Group:% c Decimal:% c", Decimal Format Symbols.getGroupingSeparator (), Decimal Format Simulations .getDecimalSeparator ())); DecimalFormatSymbols.setDecimalSeparator (','); DecimalFormatSymbols.setGroupingSeparator ('.'); DecimalCurrencyWorld.setDecimalFormatSymbols (decimalFormatSymbols); System.out.println (String.format ("After grouping:% c decimal:% c", decimal format Symbols.getGroupingSeparator (), decimalformat simulations .getDecimalSeparator ())); String Money USA = Currency USA. Format (1500.75); String Money World = MoneyWorld Format (1500.75); Decimal formatsmsgoss decimalable format symboils2 = decimalable wordworld .getDecimalFormatSymbols (); System.out.println (String.format ("Real Group:% c Decimal:% c", Decimal Format Symbols2.getGroupingSeparator (), Decimal Format Simbos 2.getDecimalSeparator ())); System.out.println (string format ("United States String:% s", moneyUSA)); System.out.println (string format ("world string:% s", moneyword); Number number USA = currency USA.parse (moneyUSA); Number number world = moneyworld.pars (moneywald); System.out.println (string format ("USA number:% F", numberUSA)); System.out.println (String.format ("World Number:% F", NumberWorld)); } When I run the above code (on my Mac OS X 10.9, Eclipse Indigo, Java 1.6), the output is:
the original group :, Decimal:. After climbing: Decimal: Real Group:. Decimal:, United States String: $ 1,500.75 World String: $ 1.500.75 USA Number: 1500.750000 World Number: 1.500000 The world currency value should be $ 1.500,75, though both locations A period was used in.
Edit:
I especially need to control the separators when I talk about the symbol of currency, I can respect the user's locale, but this Due to the requirements of the app, I need to provide the user the ability to swap duration / comma of separator time.
The problem is that I am clearly setting those values in my formulas and they are not being respected.
2 Edit: I added the code to parse the previously generated money string, to see that being respected for parsing the decimal divider, but for formatting No. It appears that during parsing, this period is still considered as a decimal separator because the world number has come out as 1.5 instead of 1500.75.
to
a currency sign; Replaced by currency symbol; If duplicated, then replaced by the international currency symbol; If a pattern is present, decimal decimal separator is used instead of a decimal separator Such:
decimal Format Symbols.setMonetaryDecimalSeparator (','); Will do this.
Comments
Post a Comment