rtroxler Posted April 19, 2007 Share Posted April 19, 2007 Why did JS return 38758.700000000004 from this calculation (3875870 * .01)? Link to comment Share on other sites More sharing options...
pulpfiction Posted April 19, 2007 Share Posted April 19, 2007 Similar problem....http://forums.devarticles.com/javascript-d...6-40t-8055.htmlhere's a discussion about floatingp oint number, [tried to understand, but couldnt] but might help you.http://www.merlyn.demon.co.uk/pas-real.htm#Float Link to comment Share on other sites More sharing options...
jesh Posted April 19, 2007 Share Posted April 19, 2007 I don't understand it well enough to explain to other people other than to say that computers aren't really able to represent floating point numbers very well. If all you are doing is multiplying by 0.01, you might consider dividing by 100 instead: <html><body><script>// outputs 38758.700000000004document.write("3875870 * .01 = " + (3875870 * .01) + "<br />");// outputs 38758.7document.write("3875870 / 100 = " + (3875870 / 100) + "<br />");</script></body></html> Link to comment Share on other sites More sharing options...
rtroxler Posted April 20, 2007 Author Share Posted April 20, 2007 Dividing by zero is what I did to resolve but it just bothered me that a basic calculation didn't return what I expected. I am fairly new to JS and I have never seen this any place else.Had a addition go funky on me too (3698850.26 + 883.34) came out as 373609.60000000003. Is there a better method or variable type to use for calculations? For the addition error, I stripped decimal position out, added the two whole numbers then divided by 100 to replace the decimal point. Sure a lot of work just to do basic math.Thanks for the input all. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now