why is java turning integers into something less
本问题已经有最佳答案,请猛点这里访问。
为什么Java把一个10的数字变成9.999999999999998?这是我的密码。此外,任何关于良好实践的建议都将受到赞赏。例如,我可以做什么来代替所有的if语句。我的代码的目的是输入一些东西的成本和支付金额。产出将是差额,以及由此产生的美元和硬币必须归还。这是我遇到困难的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | package another; import javax.swing.JOptionPane; /** * * @author Will */ public class Another { public static void tryAgain() { JOptionPane.showMessageDialog(null,"Enter a valid amount and try again"); System.exit(0); } /** * @param args the command line arguments */ public static void main(String[] args) { String purchase = JOptionPane.showInputDialog("Please enter a the purchase amount"); if (purchase == null) { System.exit(0); } if (purchase.isEmpty()) { tryAgain(); } for (int i = 0; i < purchase.length(); i = i + 1) { if (!Character.isDigit(purchase.charAt(i)) && !purchase.contains(".")) { tryAgain(); } } String given = JOptionPane.showInputDialog("Please enter the given amount"); if (given == null) { System.exit(0); } if (given.isEmpty()) { tryAgain(); } for (int i = 0; i < given.length(); i = i + 1) { if (!Character.isDigit(given.charAt(i)) && !given.contains(".")) { tryAgain(); } } Double a = Double.parseDouble(purchase); Double b = Double.parseDouble(given); Double c = b - a; if (c < 0) { JOptionPane.showMessageDialog(null,"Give the cashier more money!"); System.exit(0); } System.out.println("Change: $" + c); if (c > 100) { double hundredsPlusExtra = c / 100; double hundredsWithout = Math.floor(hundredsPlusExtra); //this line rounds down c = c - hundredsWithout * 100; if (hundredsWithout == 1) { System.out.println(hundredsWithout +" One-Hundred Dollar Bill"); } else { System.out.println(hundredsWithout +" Hundred Dollar Bills"); } } if (c > 50) { double fiftiesPlusExtra = c / 50; double fiftiesWithout = Math.floor(fiftiesPlusExtra); //this line rounds down c = c - fiftiesWithout * 50; if (fiftiesWithout == 1) { System.out.println(fiftiesWithout +" Fifty Dollar Bill"); } else { System.out.println(fiftiesWithout +" Fifty Dollar Bills"); } } if (c > 20) { double twentiesPlusExtra = c / 20; double twentiesWithout = Math.floor(twentiesPlusExtra); //this line rounds down c = c - twentiesWithout * 20; if (twentiesWithout == 1) { System.out.println(twentiesWithout +" Twenty Dollar Bill"); } else { System.out.println(twentiesWithout +" Twenty Dollar Bills"); } } if (c > 10) { double tensPlusExtra = c / 10; double tensWithout = Math.floor(tensPlusExtra); //this line rounds down c = c - tensWithout * 10; if (tensWithout == 1) { System.out.println(tensWithout +" Ten Dollar Bill"); } else { System.out.println(tensWithout +" Ten Dollar Bills"); } } if (c > 5) { double fivesPlusExtra = c / 5; double fivesWithout = Math.floor(fivesPlusExtra); //this line rounds down c = c - fivesWithout * 5; if (fivesWithout == 1) { System.out.println(fivesWithout +" Five Dollar Bill"); } else { System.out.println(fivesWithout +" Five Dollar Bills"); } } if (c > 1) { double onesPlusExtra = c / 1; double onesWithout = Math.floor(onesPlusExtra); //this line rounds down c = c - onesWithout * 1; if (onesWithout == 1) { System.out.println(onesWithout +" One Dollar Bill"); } else { System.out.println(onesWithout +" One Dollar Bills"); } } if (c > .25) { double quartersPlusExtra = c / .25; double quartersWithout = Math.floor(quartersPlusExtra); //this line rounds down c = c - quartersWithout * .25; if (quartersWithout == 1) { System.out.println(quartersWithout +" Quarter"); } else { System.out.println(quartersWithout +" Quarters"); } } } } |
你在用double计算。
请查看http://floating-point-gui.de。/
另请访问http://www.mkyong.com/java/how-do-calculate-monetary-values-in-java-double-vs-bigdecimal/
我承认,我没有读过你的代码,但99%的人确信你的二进制数据存储有问题。
你看,在十进制中,1/10可以很容易地表示为0.1。但是,尝试1/3,它变成0.333333333333333333…
在二进制中,1/10是0.000110011001100110011…
查看bigdecimal中的base10数字存储。