[Javascript] Re: INCREDIBLE IE BUG!!!!

Alberto .·. Vallini vallinis at yahoo.com
Sat Oct 25 13:07:37 CDT 2003


Hallo

I was perusing the archives upon joining when I saw
this topic. I have read 5 emails of this thread, which
I think includes 6 or 7 posts. So in case I have
missed something and I am just stating again what
maybe someone else already answered, plese forgive the
newcomer!

The problem highlighted can certainly be considered
annoying, yet it is not a bug, and actually not even a
IE thing. It is just a standard computer thing.
Numbers are represented by a computer like binary
data. This means that when you perform a decimal base
operation, the computer first translates the decimal
base into binary and then from binary to decimal again
- hoping I'm not too dumb in exposing this internal
proceeding.
In that process it is well acknowledged that deciaml
fractions can be expressed with that type of
approximation. Also Netscape and any other browser
does that.

Anyway, a rather common procedure to work around this
is the following:
given num is a floating number:

Math.round(num*1000)/1000;

that is, you multiply and then divide by a powr of ten
accordingly to how many decimals you want to keep:
1000 keeps 4 decimals, 10000 5 decimals, 10 only one
decimal.

So, case in point:
<script>
alert(0.15 + 0.015);
var rounded = Math.round( (0.15 + 0.015) *1000)/1000;
alert(rounded);
</script>

I hope this helps.
ciao


=====
------------------------------------
Alberto Vallini
http://www.unitedscripters.com/
"No one applauds a tenor because he is clearing his throat." [Les Liaisons Dangereuses]
------------------------------------

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com



More information about the Javascript mailing list