我的代碼如下:奇怪行爲
Number inputAmount = NumberFormat.getNumberInstance(Locale.FRANCE).parse("7,10");
double doubleValue = inputAmount.doubleValue();
int intValue = (int) (doubleValue * 100);
System.out.println("Input: " + inputAmount);
System.out.println("Double: " + doubleValue);
System.out.println("Integer: " + intValue);
,其輸出:
Input: 7.1
Double: 7.1
Integer: 710
但是,當inputValue
是5,10的輸出:
Input: 5.1
Double: 5.1
Integer: 509
這隻發生在5,10和4,10。我知道這是由於雙精度而發生的,但我無法弄清楚究竟如何。 任何想法?
貌似4和5不能精確表示像7可以.. – Maroun
嘗試使用的BigDecimal –
是的,我在遇到這個問題後使用了BigDecimal,並修復了它,但我想知道爲什麼第一種方法不起作用。 – user3139293