www.webdeveloper.com
Results 1 to 3 of 3

Thread: Simple Math issue with JavaScript

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    Simple Math issue with JavaScript

    I'm trying to add some numbers together with Javascript and having issues with JavaScript rounding the numbers incorrectly. Wondering if anyone knows how to work around this issue.

    Issue:
    If I enter 5.99 in first box and 5.11 in second I get 11.100000000000001 as a result, it should be 11.1. Any one know why or a work around?

    Here is a simple copy of what I'm doing.

    <HTML>
    <HEAD></HEAD>
    <script>
    function fnTotal()
    {
    i1 = text1.value - 0 ;
    i2 = text2.value - 0;
    text3.value = (i1 + i2);
    }
    </script>
    <BODY>
    <INPUT type="text" id=text1 name=text1 onchange="fnTotal()"><BR>
    <INPUT type="text" id=text2 name=text2 onchange="fnTotal()"><BR>
    <INPUT type="text" id=text3 name=text3><BR>
    </BODY>
    </HTML>

  2. #2
    Join Date
    Oct 2005
    Posts
    21
    Floating point precision errors... not much you can do about it, really. Happens in most languages. Best you can do is to format your numbers so that the effect of the errors is minimized.

  3. #3
    Join Date
    Dec 2005
    Posts
    4
    Thanks, I'll just round it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles