What are some good ways to define custom currencies, numbers, and date and time formats in GWT?

I have a GWT project in which I need to manually specify the currency formats, numbers and dates and times. These settings include specifying currency symbols, grouping seperator, decimal seperator, negative number formats, etc. What would be the best way to accomplish this?

Should I use the GWT NumberFormat class ? NumberFormat makes extensive use of GWT internationalization constructors such as Constants , etc. Therefore, if you specify a user number format mask, it will still look at the current locale and use these currency symbols, decimal symbols and thousands of delimiters, as indicated in the late and internationalized instance of "NumberFormat".

My question is: how would you do this? Could you reimplement the functionality NumberFormat? Could you get it from him and use the protected constructor and pass it to some user instance NumberConstantthat you created yourself? How to get an i18n instance of NumberConstants and use it to populate your own instance and override only what you want?

How do you approach this problem?

+3
source share
2 answers

I used NumberFormat.getFormat (String format) to format the user currency (removing the "US" before the $ sign).

. "format" i18n, .

+2
public class MyNumberFormat extends NumberFormat{

    private static CurrencyCodeMapConstants currencyCodeMapConstants = GWT.create(CurrencyCodeMapConstants.class);

    protected MyNumberFormat(String pattern, CurrencyData cdata,
            boolean userSuppliedPattern) {
        super(pattern, cdata, userSuppliedPattern);     
    }

    public static NumberFormat getCurrencyFormat(String currencyCode) {     
        return new MyNumberFormat(defaultNumberConstants.currencyPattern(),
            lookupCurrency(currencyCode), false);
      }

      private static CurrencyData lookupCurrency(String currencyCode) {
        CurrencyData currencyData = CurrencyList.get().lookup(currencyCode);

        Map currencyMap = currencyCodeMapConstants.currencyMap();       

        String code = currencyData.getCurrencyCode();
        //String symbol = currencyData.getCurrencySymbol();
        String symbol = currencyMap.get(currencyCode);
        int fractionDigits = currencyData.getDefaultFractionDigits();
        String portableSymbol = currencyData.getPortableCurrencySymbol();       

        return toCurrencyData(code, symbol, fractionDigits, portableSymbol);
      }

      public static native CurrencyData toCurrencyData(String code, String symbol, int fractionDigits, String portableSymbol) /*-{
        return [ code, symbol, fractionDigits, portableSymbol ];
      }-*/;
}

GXT

        column = new ColumnConfig("precioventa", constants.modeloPrendaPrecioVenta(), 100);
        column.setAlignment(HorizontalAlignment.RIGHT);        
        column.setNumberFormat(MyNumberFormat.getCurrencyFormat("PEN"));        
        columns.add(column);
0

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


All Articles