Results 1 to 2 of 2

Thread: JavaScript: Addding floats & formatting nicely...

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    JavaScript: Addding floats & formatting nicely...

    I have a form in HTML that has several text boxes where the users enter numbers.

    I have a textbox that is to be the sum of those numbers.

    I have javascript which parses the values entered by the user to floats, adds them, and displays them in the final text box.

    The problem is that if the users enter certain decimal values, I get odd answers.

    For example:
    Text1 = 10
    Text2 = 5
    Text3 = 20.52

    TotalText = 35.519999999999996

    How can I make TotalText show 35.52?

    Thanks!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Math.round(number) will round any value off to the nearest whole number. If you want it to round to two decimal places do:

    number = number*100
    number = Math.round(number)
    number = number/100
    Have I helped you? Please Rate my posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width