scout1idf Posted April 7, 2011 Share Posted April 7, 2011 I'm not good with JavaScript so please, if there is a simple solution/explanation, I would appreciate it. Now on to my problem.....I've been using a for loop to speed up repetitive tasks and it seems to work great, till now. <script type="text/javascript">var count=0;for (count=0;count<30;count+=[color="#FF0000"]2.4[/color]) { document.write(count+"<br />"); }</script> The results I get start out correct then go wrong. FireFox (3.6.16)____________________Results with IE 8____________________Results with Opera0________________________________0________________________________02.4______________________________2.4_______________________________2.44.8______________________________4.8_______________________________4.87.199999999999999________________7.199999999999999_________________7.29.6______________________________9.6_______________________________9.612______________________________12________________________________1214.4____________________________14.4_______________________________14.416.8____________________________16.8_______________________________16.819.2____________________________19.2_______________________________19.221.599999999999998______________21.599999999999998_________________21.6023.999999999999996______________23.999999999999996_________________23.99999999999999626.399999999999995______________26.399999999999995_________________26.39999999999999528.799999999999994______________28.799999999999993_________________28.799999999999994If I use...<script type="text/javascript">var count=0;for (count=0;count<30;count+=[color="#FF0000"]1[/color]) { document.write(count+"<br />"); }</script>OR<script type="text/javascript">var count=0;for (count=0;count<30;count+=[color="#FF0000"]1.5[/color]) { document.write(count+"<br />"); }</script> It counts like it should.So why is it when I use "2.4" it doesn't count properly? And why does it produce different results in different browsers? Link to comment Share on other sites More sharing options...
dsonesuk Posted April 7, 2011 Share Posted April 7, 2011 That is weird, but the fix is simple use count.toFixed(1). Link to comment Share on other sites More sharing options...
jeffman Posted April 7, 2011 Share Posted April 7, 2011 Not so weird. It has to do with the way floating point numbers are represented in binary. I fielded the same issue in a PHP question just yesterday, but the answer is still probably relevant. Read the Warning on this page: http://www.php.net/manual/en/language.types.float.php Link to comment Share on other sites More sharing options...
dsonesuk Posted April 7, 2011 Share Posted April 7, 2011 Yes, i did see the same report after google searching the problem, but was not 100% sure it was related, so left link out. Link to comment Share on other sites More sharing options...
scout1idf Posted April 7, 2011 Author Share Posted April 7, 2011 Thanks for the help, that worked for me..Another suggestion I got for my problem was.... for (count=0;count<30;count+=2.4) { document.write(Math.round(count*10)/10+"<br />"); } ...which works without the permanent float.Either way works for me.Thanks again. Link to comment Share on other sites More sharing options...
ShadowMage Posted April 7, 2011 Share Posted April 7, 2011 Thanks for the help, that worked for me..Another suggestion I got for my problem was....for (count=0;count<30;count+=2.4) { document.write(Math.round(count*10)/10+"<br />"); } ...which works without the permanent float.Either way works for me.Thanks again. Just so you know, this does exactly the same thing as count.toFixed(1) it just takes longer to type.BTW, what do you mean by "permanent float"? Link to comment Share on other sites More sharing options...
scout1idf Posted April 7, 2011 Author Share Posted April 7, 2011 ....BTW, what do you mean by "permanent float"?With..<script type="text/javascript">var count=0;for (count=0;count<50;count+=2.4) { document.write("Testing to see if this works for me. "+ count.toFixed(1) +" I hope it does!<br />"); }</script> When your result is say, "12" it reads as 12.0With.... <script type="text/javascript">var count=0;for (count=0;count<50;count+=2.4) { document.write("Testing to see if this works for me. "+ Math.round(count*10)/10 +" I hope it does!<br />"); }</script> The same result reads as 12 without the .0 after."permanent float" is all I could think of to describe it. Link to comment Share on other sites More sharing options...
dsonesuk Posted April 7, 2011 Share Posted April 7, 2011 Another easy fix count.toFixed(1).replace(".0","") Link to comment Share on other sites More sharing options...
thescientist Posted April 7, 2011 Share Posted April 7, 2011 With..<script type="text/javascript">var count=0;for (count=0;count<50;count+=2.4) { document.write("Testing to see if this works for me. "+ count.toFixed(1) +" I hope it does!<br />"); }</script> When your result is say, "12" it reads as 12.0With.... <script type="text/javascript">var count=0;for (count=0;count<50;count+=2.4) { document.write("Testing to see if this works for me. "+ Math.round(count*10)/10 +" I hope it does!<br />"); }</script> The same result reads as 12 without the .0 after."permanent float" is all I could think of to describe it. because that's what round does.http://www.w3schools.com/jsref/jsref_round.aspThe round() method rounds a number to the nearest integerif you are unfamiliar with the term integer, it is the same as saying a whole number. A whole number has no decimal places. Link to comment Share on other sites More sharing options...
ShadowMage Posted April 7, 2011 Share Posted April 7, 2011 With..**code**When your result is say, "12" it reads as 12.0With....**code**The same result reads as 12 without the .0 after."permanent float" is all I could think of to describe it.Ah, I see what you mean, now. Yeah that's one of the things I don't like about JavaScript. It has no decent way to round a number to a specified digits and remove trailing 0's like the PHP round function. The fix dsonesuk suggested works in this particular situation, but in situations where you have two or more decimal points you'd probably have to write a regex to do the replacing. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.