Item 60. Decimal Calculation
class Example {
public static void main(String[] args) {
System.out.println(1.03 - 0.42); // 0.6100000000000001
System.out.println(1.00 - 9 * 0.10); // 0.09999999999999998
double funds = 1.00;
int itemsBought = 0;
for (double price = 0.10; funds >= price; price += 0.10) {
funds -= price;
itemsBought++;
}
System.out.println(itemsBought + " items bought."); // 3 items bought.
System.out.println("Change: $" + funds); // Change: $0.3999999999999999
}
}Last updated