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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird JavaScript Internal Math Error

Status
Not open for further replies.

dtqbterror

Programmer
May 17, 2001
87
US
Anyone ever seen this issue with JavaScript in IE.

alert(2355.20 * 100.0)

Using this specific (2355.20) number the value returned is actually.

235519.99999999997

 
This has been quite normal for a couple decades now with floating point numbers in many programming languages. If you want, you can use

Code:
Math.round();

to round to the nearest integer, which in this case would give you the exact result you want.

Lee
 
Has always been a problem with computers. And until someone figures out a way to make a 5-way electronic switch, it will always remain a problem.

Since the computer uses 2-way switches (ON and OFF states only), ANY computation in ANY number base except 2, 4, 8, 16, 32, etc REQUIRES that all numbers and computations be rounded by the computer. Since rounding a number is almost never the same as the number that is being rounded, computational errors occur in almost every computation.

These errors have been mostly side-stepped by doing the computations in high precision, then lopping off a few bits at the end of the number to eliminate the rounding errors.

However, as you have found out, this process does NOT eliminate all rounding errors, just the vast majority.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top