我已將最大分數數字設置爲4, 但爲什麼N.doublevalue()1234.56789999999, 的輸出不是1234.5679?Java:解析NumberFormat對象並將其分配給Number對象
try{
NumberFormat NF = NumberFormat.getInstance();
NF.setMaximumFractionDigits(4);
NF.setMinimumFractionDigits(3);
Number N = NF.parse("1234.56789999999");
System.out.println(N.doubleValue());
System.out.println(NF.format(1234.567899999));
}
catch(ParseException e){e.printStackTrace();}
}
因爲你只是在打印一個沒有任何固有格式的'double' –
你有什麼特殊要求?爲了精確舍入,你應該使用BigDecimal。 – marthursson
參考這個鏈接,你會得到確切的主意http://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java – Santhucool