[Solved] In java its not calculating correctly


I suggest you not use double at all here as it is clearly confusing you.

If you are going to use double, you should always round your results instead of rounding down. Additionally as 0.1 and 0.01 and 0.05 cannot be represented accurately, you should avoid using them.

    int iPenny = (int) Math.round((iDollarTotal - iTen * 10 - iFives * 5 - iOne
            - iQuarter * 0.25 - iDime / 10.0 - iNickel / 20.0) * 100);

prints the correct result in this case.


However, I still suggest you start with.

long cents = Math.round(Double.parseDouble(tfAnswer.getText()) * 100);

after this you don’t need to use any floating point.

5

solved In java its not calculating correctly