4
試圖找出爲什麼下面的代碼不輸出期望的結果。請指教。謝謝。NumberFormat:setMaximumFractionDigits - 未給出預期結果
import java.text.*;
public class Test {
public static void main(String[] args) {
String s = "987.123456";
double d = 987.123456d;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(5);
System.out.println(nf.format(d) + " ");
try {
System.out.println(nf.parse(s));
} catch (Exception e) {
System.out.println("got exc");
}
}
}
輸出:
987.12346 // Expected 987.12345 not 987.12346
987.123456
爲什麼你想到了嗎? – 2014-10-17 01:11:41
'NumberFormat'正在四捨五入。請參閱http://stackoverflow.com/questions/3833137/how-to-make-number-format-not-to-round-numbers-up – Denise 2014-10-17 01:13:44
Sotiri,我期待這是因爲,我是在假設,因爲我設置最大分數數字爲5,對象應該返回5個不變的數字,但不知道舍入正在發生。 – 2014-10-17 15:11:03