GWT - 如何更改 NumberFormat 中的 NumberConstants
GWT - How to change NumberConstants in NumberFormat
我需要更改 NumberFormat 的 NumberConstants 中的 NaN 值和减号。
更改 NumberConstants "on the fly" 是不可能的,所以我决定继承 NumberFormat。
NumberFormat 的构造函数注释说:
@param numberConstants the locale-specific number constants to use for
this format -- NOTE subclasses passing their own instance here
should pay attention to {@link #forcedLatinDigits()} and remap
localized symbols using {@link #createLatinNumberConstants(NumberConstants)}
看来我应该调用 setForcedLatinDigits(true) 才能执行方法 createLatinNumberConstants(NumberConstants)。然后本地化常量将通过该 (createLatinNumberConstants) 方法更新,我将获得我需要的 NumberConstants。
这是我的代码:
public class MyNumberFormat extends NumberFormat {
protected MyNumberFormat(String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
this(defaul, pattern, cdata, userSuppliedPattern);
}
protected MyNumberFormat(NumberConstants numberConstants, String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
super(numberConstants, pattern, cdata, userSuppliedPattern);
}
public static MyNumberFormat getFormat(String pattern) {
return new MyNumberFormat(pattern, CurrencyList.get().getDefault(), true);
}
protected static NumberConstants createLatinNumberConstants(
final NumberConstants orig) {
final String groupingSeparator = remapSeparator(
orig.groupingSeparator());
final String decimalSeparator = remapSeparator(
orig.decimalSeparator());
final String monetaryGroupingSeparator = remapSeparator(
orig.monetaryGroupingSeparator());
final String monetarySeparator = remapSeparator(
orig.monetarySeparator());
return new NumberConstants() {
@Override
public String currencyPattern() {
return orig.currencyPattern();
}
@Override
public String decimalPattern() {
return orig.decimalPattern();
}
@Override
public String decimalSeparator() {
return decimalSeparator;
}
@Override
public String defCurrencyCode() {
return orig.defCurrencyCode();
}
@Override
public String exponentialSymbol() {
return orig.exponentialSymbol();
}
@Override
public String globalCurrencyPattern() {
return orig.globalCurrencyPattern();
}
@Override
public String groupingSeparator() {
return groupingSeparator;
}
@Override
public String infinity() {
return orig.infinity();
}
@Override
public String minusSign() {
return UnicodeSymbols.MINUS;
}
@Override
public String monetaryGroupingSeparator() {
return monetaryGroupingSeparator;
}
@Override
public String monetarySeparator() {
return monetarySeparator;
}
@Override
public String notANumber() {
return "?";
}
@Override
public String percent() {
return orig.percent();
}
@Override
public String percentPattern() {
return orig.percentPattern();
}
@Override
public String perMill() {
return orig.perMill();
}
@Override
public String plusSign() {
return orig.plusSign();
}
@Override
public String scientificPattern() {
return orig.scientificPattern();
}
@Override
public String simpleCurrencyPattern() {
return orig.simpleCurrencyPattern();
}
@Override
public String zeroDigit() {
return "0";
}
};
}
}
因此,为了格式化数字,我将执行此操作:
String fmt(final Double x){
MyNumberFormat.setForcedLatinDigits(true);
MyNumberFormat format = MyNumberFormat.getFormat("0.0000");
return format.format(x)
}
但实际上createLatinNumberConstants是受保护的静态方法,不能被覆盖或替换。
所以 MyNumberFormat.createLatinNumberConstants() 永远不会被执行。
我做错了什么?
我认为您不需要覆盖 createLatinNumberConstants
- 而是将数字常量传递给超级 class 的构造函数。
作为基础,您可以使用 NumberFormat
.
的默认数字常量
类似的东西应该可以工作:
public class MyNumberFormat extends NumberFormat {
protected MyNumberFormat(String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
super(createMyNumberConstants(NumberFormat.defaultNumberConstants), pattern, cdata, userSuppliedPattern);
}
public static MyNumberFormat getFormat(String pattern) {
return new MyNumberFormat(pattern, CurrencyList.get().getDefault(), true);
}
protected static final NumberConstants createMyNumberConstants(
final NumberConstants orig) {
return new NumberConstants() {
@Override
public String currencyPattern() {
return orig.currencyPattern();
}
@Override
public String decimalPattern() {
return orig.decimalPattern();
}
@Override
public String decimalSeparator() {
return decimalSeparator();
}
@Override
public String defCurrencyCode() {
return orig.defCurrencyCode();
}
@Override
public String exponentialSymbol() {
return orig.exponentialSymbol();
}
@Override
public String globalCurrencyPattern() {
return orig.globalCurrencyPattern();
}
@Override
public String groupingSeparator() {
return orig.groupingSeparator();
}
@Override
public String infinity() {
return orig.infinity();
}
@Override
public String minusSign() {
return orig.minusSign();
}
@Override
public String monetaryGroupingSeparator() {
return orig.monetaryGroupingSeparator();
}
@Override
public String monetarySeparator() {
return orig.monetarySeparator();
}
@Override
public String notANumber() {
return "?";
}
@Override
public String percent() {
return orig.percent();
}
@Override
public String percentPattern() {
return orig.percentPattern();
}
@Override
public String perMill() {
return orig.perMill();
}
@Override
public String plusSign() {
return orig.plusSign();
}
@Override
public String scientificPattern() {
return orig.scientificPattern();
}
@Override
public String simpleCurrencyPattern() {
return orig.simpleCurrencyPattern();
}
@Override
public String zeroDigit() {
return "0";
}
};
}
我需要更改 NumberFormat 的 NumberConstants 中的 NaN 值和减号。 更改 NumberConstants "on the fly" 是不可能的,所以我决定继承 NumberFormat。
NumberFormat 的构造函数注释说:
@param numberConstants the locale-specific number constants to use for this format -- NOTE subclasses passing their own instance here should pay attention to {@link #forcedLatinDigits()} and remap localized symbols using {@link #createLatinNumberConstants(NumberConstants)}
看来我应该调用 setForcedLatinDigits(true) 才能执行方法 createLatinNumberConstants(NumberConstants)。然后本地化常量将通过该 (createLatinNumberConstants) 方法更新,我将获得我需要的 NumberConstants。
这是我的代码:
public class MyNumberFormat extends NumberFormat {
protected MyNumberFormat(String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
this(defaul, pattern, cdata, userSuppliedPattern);
}
protected MyNumberFormat(NumberConstants numberConstants, String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
super(numberConstants, pattern, cdata, userSuppliedPattern);
}
public static MyNumberFormat getFormat(String pattern) {
return new MyNumberFormat(pattern, CurrencyList.get().getDefault(), true);
}
protected static NumberConstants createLatinNumberConstants(
final NumberConstants orig) {
final String groupingSeparator = remapSeparator(
orig.groupingSeparator());
final String decimalSeparator = remapSeparator(
orig.decimalSeparator());
final String monetaryGroupingSeparator = remapSeparator(
orig.monetaryGroupingSeparator());
final String monetarySeparator = remapSeparator(
orig.monetarySeparator());
return new NumberConstants() {
@Override
public String currencyPattern() {
return orig.currencyPattern();
}
@Override
public String decimalPattern() {
return orig.decimalPattern();
}
@Override
public String decimalSeparator() {
return decimalSeparator;
}
@Override
public String defCurrencyCode() {
return orig.defCurrencyCode();
}
@Override
public String exponentialSymbol() {
return orig.exponentialSymbol();
}
@Override
public String globalCurrencyPattern() {
return orig.globalCurrencyPattern();
}
@Override
public String groupingSeparator() {
return groupingSeparator;
}
@Override
public String infinity() {
return orig.infinity();
}
@Override
public String minusSign() {
return UnicodeSymbols.MINUS;
}
@Override
public String monetaryGroupingSeparator() {
return monetaryGroupingSeparator;
}
@Override
public String monetarySeparator() {
return monetarySeparator;
}
@Override
public String notANumber() {
return "?";
}
@Override
public String percent() {
return orig.percent();
}
@Override
public String percentPattern() {
return orig.percentPattern();
}
@Override
public String perMill() {
return orig.perMill();
}
@Override
public String plusSign() {
return orig.plusSign();
}
@Override
public String scientificPattern() {
return orig.scientificPattern();
}
@Override
public String simpleCurrencyPattern() {
return orig.simpleCurrencyPattern();
}
@Override
public String zeroDigit() {
return "0";
}
};
}
}
因此,为了格式化数字,我将执行此操作:
String fmt(final Double x){
MyNumberFormat.setForcedLatinDigits(true);
MyNumberFormat format = MyNumberFormat.getFormat("0.0000");
return format.format(x)
}
但实际上createLatinNumberConstants是受保护的静态方法,不能被覆盖或替换。 所以 MyNumberFormat.createLatinNumberConstants() 永远不会被执行。
我做错了什么?
我认为您不需要覆盖 createLatinNumberConstants
- 而是将数字常量传递给超级 class 的构造函数。
作为基础,您可以使用 NumberFormat
.
类似的东西应该可以工作:
public class MyNumberFormat extends NumberFormat {
protected MyNumberFormat(String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
super(createMyNumberConstants(NumberFormat.defaultNumberConstants), pattern, cdata, userSuppliedPattern);
}
public static MyNumberFormat getFormat(String pattern) {
return new MyNumberFormat(pattern, CurrencyList.get().getDefault(), true);
}
protected static final NumberConstants createMyNumberConstants(
final NumberConstants orig) {
return new NumberConstants() {
@Override
public String currencyPattern() {
return orig.currencyPattern();
}
@Override
public String decimalPattern() {
return orig.decimalPattern();
}
@Override
public String decimalSeparator() {
return decimalSeparator();
}
@Override
public String defCurrencyCode() {
return orig.defCurrencyCode();
}
@Override
public String exponentialSymbol() {
return orig.exponentialSymbol();
}
@Override
public String globalCurrencyPattern() {
return orig.globalCurrencyPattern();
}
@Override
public String groupingSeparator() {
return orig.groupingSeparator();
}
@Override
public String infinity() {
return orig.infinity();
}
@Override
public String minusSign() {
return orig.minusSign();
}
@Override
public String monetaryGroupingSeparator() {
return orig.monetaryGroupingSeparator();
}
@Override
public String monetarySeparator() {
return orig.monetarySeparator();
}
@Override
public String notANumber() {
return "?";
}
@Override
public String percent() {
return orig.percent();
}
@Override
public String percentPattern() {
return orig.percentPattern();
}
@Override
public String perMill() {
return orig.perMill();
}
@Override
public String plusSign() {
return orig.plusSign();
}
@Override
public String scientificPattern() {
return orig.scientificPattern();
}
@Override
public String simpleCurrencyPattern() {
return orig.simpleCurrencyPattern();
}
@Override
public String zeroDigit() {
return "0";
}
};
}