以下为Excel中有效的自定义编号格式:#,## 0.0,
请注意结尾的逗号。它允许Excel将数字按比例缩小1000;
类似地,末尾的双逗号减少了百万。
但是,在Java中,当我创建这种格式时使用DecimalFormat(如下代码所示,它会引发异常消息:格式错误的格式“#,## 0.0,”] >>
String format="#,##0.0,"; DecimalFormat df = new DecimalFormat(format);
是否有任何方法可以创建将执行此缩减的DecimalFormat?
例如,如果我在Excel中将相同的模式应用于数字705.0,则Excel中显示的结果为0.7
这是我期望在执行后作为输出:
String result = df.format(new Double(705.0));
如果没有办法,我希望我可以通过在应用格式:#,## 0.0之前先将其除以1000来获得结果。>
以下是Excel中有效的自定义编号格式:#,## 0.0,请注意结尾的逗号。它允许Excel将数字缩小1000。类似地,在末尾用双逗号分隔下一个数字...
最简单的解决方法是只创建自己的NumberFormat子类,该子类在内部使用DecimalFormat并跟踪那些结尾的逗号。有点像
public class ExcelFormat extends NumberFormat {
private DecimalFormat df;
private int endingCommas;
public ExcelFormat(String format) {
//calculate the ending commas. Remove from format
//create df using the substring with the commas removed
}
public String format(double number, StringBuffer buf, FieldPosition fp) {
//divide the number by what's indicated by the endingCommas
return df.format(adjustedNumber);
}