You are here: irt.org | About | Feedback | 3796 [ previous next ]
Feedback on:
irt.org FAQ Knowledge Base Q1072
Sent by
Simon Adamyk on April 21, 2002 at 13:36:05:
Worth:
Very worth reading
Length:
Too short
Technical:
Not technical enough
Comments:
First, thanks very much INDEED for a first class website which has provided me with great inspiration and helped me out of many a tight JavaScripting corner. It is truly excellent, and you have my admiration.
My question is whether this rounding error is one which also occurs in IE 5.5 and IE6? I used the outputCents function from FAQ 723 but it seemed to produce incorrect rounding in some circumstances but not in others (which puzzles me). Consider the following mini-script to demonstrate the problem:
<html>
<script language="JavaScript">
function outputCents(amount) {
amount = Math.round( ( (amount) - Math.floor(amount) ) *100);
return (amount < 10 ? '.0' + amount : '.' + amount);
}
document.write("The decimal part of 1.5 rounded to two decimal places is " + outputCents(1.5) + "
document.write("The decimal part of 1.51 rounded to two decimal places is " + outputCents(1.51) + "<br>");
document.write("The decimal part of 1.555 rounded to two decimal places is " + outputCents(1.555) + "<br>");
document.write("The decimal part of 1.549 rounded to two decimal places is " + outputCents(1.549) + "<br>");
document.write("The decimal part of 1.551 rounded to two decimal places is " + outputCents(1.551) + "<br>");
document.write("The decimal part of 123.555 rounded to two decimal places is " + outputCents(123.555) + "<br>");
</script>
</html>
Note particularly the difference between the 1.555 and 123.555 outputs. Maybe it's me being stupid, I don't know. Anyway, I can't explain all this and thought you might be interested.
With my thanks again,
Simon Adamyk