This content has been marked as final. Show 6 replies
Math.floor(100d * amount + 0.5d) / 100d;
If you must use double for numbers (which is a bad idea), look up DecimalFormat.
What Every Computer Scientist Should Know About Floating-Point Arithmetic
If you must use double for numbers (which is a badNot necessarily, depending on how closely you count your pennies.
idea), look up DecimalFormat.
And read:It is true that there are times when the code I used will not do exactly what it says on the tin, however I have found that coupling it with a DecimalFormat when writing the result to output is more than sufficient.
html ]What Every Computer Scientist Should Know About
But yes, you are correct, when absolute accuracy is key, doubles (and my code) will at times produce an inaccurate result.