首先,您无需更改 (BigDecimal) 元素的存储方式!
您必须更改它们的显示方式。您可以使用DecimalFormat类来做到这一点。
例如:
private static final String BIG_DECIMAL_DEFAULT_FORMAT = "##0.00";
private static final char GROUPING_SEPARATOR = '.';
private static final char DECIMAL_SEPARATOR = ',';
private static final int DIGIT_GROUP_NUMBER = 3;
private static final DecimalFormat decimalFormatter = new DecimalFormat(BIG_DECIMAL_DEFAULT_FORMAT);
static{
decimalFormatter.setParseBigDecimal(true);
decimalFormatter.setGroupingSize(DIGIT_GROUP_NUMBER);
paschiFaceSymbols.setDecimalSeparator(DECIMAL_SEPARATOR);
paschiFaceSymbols.setGroupingSeparator(GROUPING_SEPARATOR);
}
比您还必须实现一种方法来显示 BigDecimal 值。例如:
public static String format(BigDecimal value, String pattern)
{
if (value == null){
throw new IllegalArgumentException("Error:value=null");
}
if (pattern != null && !pattern.isEmpty()){
decimalFormatter.applyPattern(pattern);
}
else{
decimalFormatter.applyPattern(BIG_DECIMAL_DEFAULT_FORMAT);
}
return decimalFormatter.format(value);
}