Problem when adding decimal values with parseFloat

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RajeshVoruganti
    New Member
    • Apr 2012
    • 1

    #1

    Problem when adding decimal values with parseFloat

    When i am adding the two decimal values using with java script

    var first = 0.1;
    var second = 0.2;

    var tot = parseFloat(firs t) + parseFloat(seco nd);

    i am expecting result as : 0.3

    But it is giving result as : 0.3000000000000 0004


    Please let me know if anyone know the solution.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    there is no solution. that is a limitation of floating point arithmetics. all you could do is round the value to a desired number of digits.

    Comment

    • roshan ban
      New Member
      • Apr 2012
      • 21

      #3
      try parseDouble(tot ); at the end of your result

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        parseDouble() is not a built-in JavaScript function.

        Comment

        Working...