Hello guys,
Can anyone find something in this code strange. It pops out an unexpected 700.0000001 meanwhile all others put 500.00, 400.00,300.00
Result:
The interest at 1 is 100.0
The interest at 2 is 200.0
The interest at 3 is 300.0
The interest at 4 is 400.0
The interest at 5 is 500.0
The interest at 6 is 600.0
The interest at 7 is 700.0000000000001
The interest at 8 is 800.0
The interest at 9 is 900.0
The interest at 10 is 1000.0
The code is:
double amount = 10000;
for(int interestRate = 1; interestRate < 11; interestRate++){
double result = calculateInterest(10000, interestRate);
System.out.println("The interest at " + interestRate + " is " + result );
}
It uses this method:
public static double calculateInterest(double amount, double interestRate) {
return(amount * (interestRate / 100));
}
Any thought ?
--- Update ---
Solution is
String.format("%.2f", )
System.out.println("The interest at " + interestRate + " is " + String.format("%.2f", result ));