代码之家  ›  专栏  ›  技术社区  ›  Pieter Breed

在GWT中,指定自定义货币、数字和日期时间格式的好方法是什么?

  •  3
  • Pieter Breed  · 技术社区  · 16 年前

    我有一个GWT项目,其中需要手动指定货币、数字和日期时间格式。这些自定义包括指定货币符号、分组分隔符、小数分隔符、负数格式等。实现这一点的最佳方法是什么?

    我应该用GWT吗 NumberFormat 班级?数字格式广泛使用了GWT国际化结构,如 Constants 接口等。因此,如果指定自定义数字格式掩码,它仍将查看当前区域设置,并使用这些货币符号、十进制符号和千位分隔符,如后期绑定和国际化的“数字格式”实例中所指定的。

    我的问题是:如何 完成这一点?你能重新实施吗 NumberFormat 的功能?您是否可以从中派生并使用受保护的构造函数,并以某种自定义方式传递它? NumberConstant 您自己创建的实例?如何获取I18N NumberConstants实例,并使用该实例填充您自己的实例并仅覆盖您想要的内容?

    你将如何处理这个问题?

    2 回复  |  直到 13 年前
        1
  •  2
  •   nomad    15 年前

    我使用了numberFormat.getFormat(字符串格式)来设置自定义货币格式(在$sign之前删除“us”)。

    在我的应用程序中,我将结果放入公共常量。我已经将“format”参数存储在i18n资源包以及所有特定于UI的字符串中。

        2
  •  0
  •   juan    13 年前
    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);