class rounding
float nearestTenPensFailure = Math.round((myMoney * 100 / 10)) * 10 / 100; //without the 'f' definition.
System.out.println(nearestTenPensFailure); //This returns the wrong rounded value.
//For the next lesson, ask why.
You can simplify the math in your code to the following:
float nearestTenPensFailure = Math.round((myMoney * 10)) / 10f;
//___________________________________________________________________^_
//then you see the answer to why is this needed is because if this is not declared as a float it's taken for an 'integer division.'
//Edit
//Declaring the result variable as double/float just causes an implicit conversion to occur after division.
//Ant it always rounds down. int i = 0.99999999 sets int to 0. More specifically, it takes the integer portion and discards the rest.
see here: https://stackoverflow.com/questions/7286681/why-does-integer-division-code-give-the-wrong-answer
Edited by Emanuel Graf