JRS: In article <11*********************@z14g2000cwz.googlegroups. com>,
dated Wed, 9 Nov 2005 10:01:45, seen in news:comp.lang.javascript,
harborcoat-design <tj******@gmail.com> posted :
I thought I was reasonably versed in JavaScript, buuuuut...
Without reading the newsgroup FAQ, as posted every Mon & Fri ??
When I do the following:
x = parseFloat(326.655);
y = parseFloat(100);
z = x * y;
alert(z);
When I should get 32665.5 I instead get 32665.499999999996.
Why is that?
After the FAQ, read <URL:http://www.merlyn.demon.co.uk/js-maths.htm> and
links.
Note that 100 will be stored exactly, but it is not a power of 2.
326.655 cannot be stored exactly, so multiplication by 100 loses some
bits of the ideal result, giving something that outputs as you see.
But 0.01 cannot be stored exactly, so dividing by 0.01 will not
*necessarily* give the same result, although in this case it does.
AFAICS, parseFloat is worth using only when its parameter can be a
string with other characters after the number.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.