我需要在NumberFormat的NumberConstants中更改NaN值和减号。 “动态”更改NumberConstants是不可能的,所以我决定将NumberFormat子类化。
NumberFormat的构造函数注释说:
@param numberConstants要使用的特定于语言环境的数字常量 这种格式 - 注意子类在这里传递自己的实例 应该注意{@link #forcedLatinDigits()}并重新映射 使用{@link #createLatinNumberConstants(NumberConstants)}
的本地化符号
在我要执行的方法createLatinNumberConstants(NumberConstants)中,我应该调用setForcedLatinDigits(true)。然后,本地化的常量将由(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()。
我做错了什么?
答案 0 :(得分:0)
我认为你不需要覆盖createLatinNumberConstants
- 而是将你的数字常量传递给超类的构造函数。
作为基础,您可以使用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";
}
};
}