Thanks for the reply Rothrock, but that does seem strange. If you add 0.1 to
something, it should go up by that. Or is my maths really bad?
I don't really know how to get the output I'm after, as sometimes you would
need to round down and other up. Does anybody know what the code should be to
fix this?
Thanks
hutn.jimbo,
> Thanks for the reply Rothrock, but that does
> seem strange. If you add 0.1 to something, it
> should go up by that.
In a theoretical sense, of course, you're right. The problem isn't with
your arithmetic ability, but rather with the basic problem of how computers
represent decimal values. I wish I could explain it better than that (it
definitely surprised me when I first saw it), but you do get used to it.
The same output occurs in JavaScript:
<script>
var counter = 0;
for (var i=0; i<10; i++) {
document.write((counter += 0.1) + "<br />");
}
</script>
> I don't really know how to get the output I'm after,
> as sometimes you would need to round down and
> other up.
I can think of two ways in this scenario. Either multiply your value by
10, round, then divide it by 10 (or whatever decimal place makes sense) ...
var myNum:Number = 0;
for (var i:Number = 0; i < 10; i++) {
myNum *= 10;
myNum += 1;
myNum /= 10;
trace(myNum);
}
... which is your only choice (I think) with ActionScript 2.0. In
JavaScript, you could use the Number.toFixed() method:
<script>
var counter = 0;
for (var i=0; i<10; i++) {
document.write((counter += 0.1).toFixed(1) + "<br />");
}
</script>
David Stiller
Co-author, Foundation Flash CS4 for Designers
http://tinyurl.com/5j55cv
"Luck is the residue of good design."