JavaScript parseFloat

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SSG001
    New Member
    • Oct 2007
    • 110

    JavaScript parseFloat

    Code:
    x=0.4;
    y=0.200;
    z=parseFloat(x)+parseFloat(y);
    alert(z);
    //z value is shown as 2.400000002


    but it works with 0.5+0.200 i gives correct answer

    Thanks
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Works fine for me. Are you sure you used .2 in the original code and not 2? I got the right answer when I used the code you posted.

    Comment

    • SSG001
      New Member
      • Oct 2007
      • 110

      #3
      sorry made a mistake while posting my question
      instead of x,y variables if the input is taken from html input boxes like this where x value in input box is 0.4 and

      Code:
      x=parseFloat(eval(document.getElementById("qty_in"+i)).value);
      
      
      y=parseFloat(eval(document.getElementById("qty"+i)).value);
      z=parseFloat(x)+parseFloat(y);

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        In that case, we would need to see the HTML and what values were in the controls at the time the code was run.

        Comment

        Working...