Using Float / decimal in Jscript & JavaScript

Thread: Using Float / decimal in Jscript & JavaScript

1. Junior Member
Join Date
Dec 1969
Posts
4

Using Float / decimal in Jscript & JavaScript

This could seem banal, but I need to substract 2 float numbers, but I don&#039;t seem to get the proper answer:<BR><BR>2.5 - 2.4 = 0.10000000000000009<BR><BR>even if I use <BR>var answer = 2.5 - 2.4<BR>or the eval function, still doesn&#039;t work<BR>isn&#039;t it suppose to give me 0.1 ???<BR><BR>thanks for any advices,<BR><BR>--<BR>ejparle<BR>Montreal, Can, Go Habs Go!

2. Senior Member
Join Date
Dec 1969
Posts
96,118

No different than other languages...

Welcome to the world of binary computers!<BR><BR>Just as you can&#039;t represent 1/3 exactly in decimal notation (you end up with an infinite string of digits, 0.333333333....), you can&#039;t represent 1/10 exactly in binary notation, as used inside 99% of all computers in use today. (Okay, maybe there are enough old COBOL programs out there that it&#039;s only 90% or so... But aside from COBOL and a handful of other old languages...)<BR><BR>So the answer you are getting is as close as the computer can compute it, given the limitations of binary arithmetic.<BR><BR>You can&#039;t *EVER* get *exactly* an answer of 0.1 with float/double numbers. You can get close.<BR><BR>

3. Junior Member
Join Date
Dec 1969
Posts
4

I don't get it...

I just tried that in c++:<BR><BR>main()<BR>{<BR> float z = 2.5 - 2.4;<BR> cout &#060;&#060; "z: " &#060;&#060; z &#060;&#060; endl;<BR>}<BR><BR>and when I execute the program, I get:<BR>&#062; z: 0.1<BR><BR>that&#039;s what I need in JavaScript, but that&#039;s not what I get...<BR>if it&#039;s not possible to have 0.1 as the result of 2.5 - 2.4 , is<BR>there a function to truncate the result to the significant number, or I need to convert it to a string and truncate it myself? I know about the round function, but I need something like this for float.

4. Senior Member
Join Date
Dec 1969
Posts
96,118

Because your C++ test is bogus

C++ is simply ROUNDING THE DISPLAYED result!<BR><BR>C++ is *LYING* to you!<BR><BR>Hard to believe you learned C++ and never learned the fundamentals of floating point arithmetic. (I don&#039;t blame you; I blame whomever or whatever you learned from. Because it&#039;s such a very very important topic!)<BR><BR>Try this in C++:<BR><BR>main( )<BR>{<BR>&nbsp; &nbsp; double tenth = 2.5 - 2.4;<BR>&nbsp; &nbsp; double sum = 0.0;<BR>&nbsp; &nbsp; for ( int i = 1; i &#060;= 100; ++i ) sum += tenth;<BR>&nbsp; &nbsp; cout &#060;&#060; "sum: " &#060;&#060; sum &#060;&#060; endl &#060;&#060; "sum minus 10: " &#060;&#060; (sum-10.0) &#060;&#060; endl;<BR>}<BR><BR>See what *that* shows you!<BR><BR>cout in C++, especially, does a good job of rounding off numbers and thus lying to you. VBScript, incidentally, does the same thing. JavaScript is an unsophisticated language, so it doesn&#039;t lie to you.<BR><BR><BR>

5. Junior Member
Join Date
Dec 1969
Posts
4

now I get it, but then how can javascript lie to m

ok, I get it... so then how do I make javascript lie to me?<BR>Is there a function or something to end up with the answer 0.1?<BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

Well...

...you&#039;ll never get a *NUMERICAL* answer of 0.1, as I noted.<BR><BR>But you can get JS to give you an answer accurate to N digits, which is really what C++ is doing with cout.<BR><BR>If you know how many digits you want, you can do something like this:<BR><BR>function RoundTwo( num )<BR>{<BR>&nbsp; &nbsp; var sgn = num &#060; 0 ? "-" : "";<BR>&nbsp; &nbsp; num = Math.round( Math.abs( 100 * num ) );<BR>&nbsp; &nbsp; if ( num &#060; 10 ) return sgn + "0.0" + num;<BR>&nbsp; &nbsp; if ( num &#060; 100 ) return sgn + "0." + num;<BR>&nbsp; &nbsp; num = sgn + num; // convert it to a string<BR>&nbsp; &nbsp; return num.substring(0,num.length-2) + "." + num.substring(num.length-2);<BR>}<BR><BR>It&#039;s easy to create a function that rounds to any specific number of decimal places. It&#039;s tougher to create one that rounds to an arbitrary number, but not at all impossible. I&#039;ll point you to:<BR>http://www.irt.org and the JavaScript FAQ<BR>and<BR>http://javascript.internet.com [sister site of this place]<BR><BR>You can surely find functions to do most anything you want at those two (and other JS) sites.<BR><BR>

7. Junior Member
Join Date
Dec 1969
Posts
4

Thanks!

Thanks a lot, you&#039;ve been very helpfull. I&#039;ll go check those links. :-)<BR><BR>--<BR>ejparle

8. Senior Member
Join Date
Dec 1969
Posts
11,334

It got me as well last year... you may want to look at why it happens:<BR><BR>http://www.aspmessageboard.com/forum/showMessage.asp?F=27&M=278406&P=1

9. Senior Member
Join Date
Dec 1969
Posts
96,118

You do keep so odd links...

...handy, don&#039;t you?<BR><BR>Maybe I should package that sequence up into an article. Only a hundred people would read it--and only seven would care--but it might be fun, anyway.<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
11,334

RE: You do keep so odd links...

Actually, I had to search for it... just searched for "***" in the advanced forum.<BR><BR>You could make it into an article, but since the majority of people on here use VB/VBS, it&#039;s probably not going to make a difference to &#039;em.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•