How to specify thousands and decimal separator used by GWT NumberFormat

In a GWT class document NumberFormat (http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/i18n/client/NumberFormat.html) I read:

"Prefixes, suffixes and various characters used for infinity, numbers, thousands separators, decimal separators , etc. can be set to arbitrary values and they will be displayed correctly during formatting. However, care must be taken to ensure that characters and strings do not conflict, or parsing will be unreliable. For example, the decimal separator and the thousands separator must be different characters, or parsing will be impossible. "

My question is: how can I make sure that "." is used as a separator of thousands and "," as a separator of decimal numbers, regardless of the user's locale? In other words, when I use the template "###, ###, ###. ######" I want the GWT to format the double value 1234567.89 always as "1.234.567, 89" no matter what custom language exists.

+6
source share
3 answers

Solving this required some work. From the docs and source for NumberFormatter, it looks like for example, only locales can be used to set these values. They say you can set the group separator , but such examples did not work for me. Although you might think that the Java way of doing this below will work, since the GWT emulates DecimalFormat and DecimalFormalSymbols , they do not officially support them. Perhaps they will be in the future. In addition, they say in the LocaleInfo class that you can change the locale, I have not found such methods to allow this.

So here is the Hack way of doing this :

NumberFormat.getFormat("#,##0.0#").format(2342442.23d).replace(",", "@"); 

Correct, but not yet supported by GWT :

Use the decimal formatter:

  // formatter DecimalFormat format= new DecimalFormat(); // custom symbol DecimalFormatSymbols customSymbols=new DecimalFormatSymbols(); customSymbols.setGroupingSeparator('@'); format.setDecimalFormatSymbols(customSymbols); // test String formattedString = format.format(2342442.23d); 

Exit:

2 @ 342 @ 442.23

+4
source

I just ran into the same problem. I solved it like this:

 public String formatAmount(Double amount) { String pattern = "#,##0.00"; String groupingSeparator = LocaleInfo.getCurrentLocale().getNumberConstants().groupingSeparator(); String decimalSeparator = LocaleInfo.getCurrentLocale().getNumberConstants().decimalSeparator(); NumberFormat format = NumberFormat.getFormat(pattern); return format.format(amount).replace(groupingSeparator, "'").replace(decimalSeparator, "."); } 
+3
source

The way I used overrides GWT LocaleInfoImpl as follows:

  • Looking at LocaleInfo, you can see that it uses a private static instance of LocaleInfo line 36 , which it builds with GWT.create(LocaleInfoImpl.class)
  • Using the GWT Deferred Binding , we can override LocalInfoImpl with a special implementation:
 <replace-with class="your.app.package.to.CustomLocaleInfoImpl"> <when-type-is class="com.google.gwt.i18n.client.impl.LocaleInfoImpl" /> </replace-with> 
  1. Extend LocaleInfoImpl in the same way by simply overriding the getNumberConstant method:
 public class CustomLocaleInfoImpl extends LocaleInfoImpl { @Override public NumberConstants getNumberConstants() { final NumberConstants nc = super.getNumberConstants(); return new NumberConstants() { @Override public String notANumber() { return nc.notANumber(); } @Override public String currencyPattern() { return nc.currencyPattern(); } @Override public String decimalPattern() { return nc.decimalPattern(); } @Override public String decimalSeparator() { return nc.decimalSeparator(); } @Override public String defCurrencyCode() { return nc.defCurrencyCode(); } @Override public String exponentialSymbol() { return nc.exponentialSymbol(); } @Override public String globalCurrencyPattern() { return nc.globalCurrencyPattern(); } @Override public String groupingSeparator() { return "@";//or any custom separator you desire } @Override public String infinity() { return nc.infinity(); } @Override public String minusSign() { return nc.minusSign(); } @Override public String monetaryGroupingSeparator() { return nc.monetaryGroupingSeparator(); } @Override public String monetarySeparator() { return nc.monetarySeparator(); } @Override public String percent() { return nc.percent(); } @Override public String percentPattern() { return nc.percentPattern(); } @Override public String perMill() { return nc.perMill(); } @Override public String plusSign() { return nc.plusSign(); } @Override public String scientificPattern() { return nc.scientificPattern(); } @Override public String simpleCurrencyPattern() { return nc.simpleCurrencyPattern(); } @Override public String zeroDigit() { return nc.zeroDigit(); } }; } } 
0
source

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


All Articles