[Javascript] (0.9 + 0.1).toFixed(2) = 0.0:

SineCosine

I'm still looking for my Tangent
Reaction score
77
Umm..
Yea, I was just making some random vernier calipers out of javascript because explaining how to use them to some people is just difficult =/

So, making it was easy.
It works fine and all, nothing wrong with that script.

The problem is these 'buttons' I added that will increase the reading by 0.01cm.
The button:
(Nothing wrong here)
Code:
<input type="button" value=">>>>" onClick="increaseReading()">
<input type="text" value="0.00" id="reading">
<div id="Vernier"></div>

The script:
Code:
var observedReading = 0.00;
function increaseReading () {
    var r = document.getElementById('reading');
    observedReading = (parseFloat(r.value, 10) + 0.01).toFixed(2);
    r.value = observedReading;

    createVernier(document.getElementById('Vernier'), observedReading);
}

So, there are 2 ways for the user to increase the reading of the vernier calipers.
  1. Entering a value in the text-input field
  2. Clicking the '>>>>' button

Using the text-input field works with no errors.
Clicking the button results in the weird bug where [lJASS]0.09 + 0.01 = 0.0:[/lJASS]
But only for values with the number 9 at it's hundredths-place.

Code:
//I tried this
alert((0.09 + 0.01).toFixed(2));

//The result is -> 0.0:

Weird =/
Anyone know what's wrong?

PS: It won't work if I do this:
Code:
createVernier(someDiv, (0.09 + 0.01);

function createVernier (parentDiv, reading) {
    alert(reading.toFixed(2));
    //It'll still return 0.0:
}
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top