Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<script> tt=10 pprice=6.64 alert

Status
Not open for further replies.

Phil79

Technical User
Jun 5, 2002
64
GB
<script>
tt=10
pprice=6.64
alert(pprice*tt)
</script>

I'm using IE6 and in my browser this returns 66.39999999. Please could someone explain why it doesn't return 66.4? Is it a bug in IE?
 
it is impossible for some numbers to be represented exactly in IEEE format, which is what javascript uses for floating point numbers. This is not a &quot;bug&quot; perse, but rather a limitation of representing floating point numbers in a finite set of bits. These sorts of floating-point roundoff errors are present in all programming languages as far as i'm aware (?) but i could be wrong.

you might want to use a round function for presentation of your numbers.

cheyney
 
Try this:

var tt=10
var pprice=6.64
var multi = pprice*tt
var value = multi.toFixed(2)
alert(value)

or just

var tt=10
var pprice=6.64
var multi = pprice*tt
alert(multi.toFixed(2))

BTW, my reference say that 'toFixed' converts a number to a a string so beware it's a string.

To keep make it a number again you can do something like:

var value = parseFloat(multi.toFixed(2))

But in that case the argument in 'toFixed(argument)' determines the number of digits after the decimal point and rounds the trailing digits as appropiate.

Hope this helps,
Erik
<-- My sport: Boomerang throwing !!
!! Many Happy Returns !! -->
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top