+ Reply to Thread
Results 1 to 3 of 3
Like Tree3Likes
  • 1 Post By hrvat
  • 2 Post By misson

Thread: Javascript dividing by 100 giving long decimal numbers

  1. #1
    joshy's Avatar
    joshy is offline x10Hosting Member joshy is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    8

    Question Javascript dividing by 100 giving long decimal numbers

    I have been working on a calculation website for school. I have to divide numbers by 100 to convert from percentage to decimal. With some numbers, I get continuous decimal numbers, which by a normal calculator, doesn't happen.

    Here's some examples (good and bad):
    Code:
    0.13 / 100 = 0.0013 (good)
    0.14 / 100 = 0.0014000000000000002 (bad)
    0.141 / 100 = 0.0014099999999999998 (bad)
    0.145 / 100 = 0.00145 (good)
    0.67 / 100 = 0.0067 (good)
    0.68 / 100 = 0.0068000000000000005 (bad)
    Also, if multiplied them by 10 (making 0.68 become 6.8 for example) then divided by 1000:
    Code:
    0.68 * 10 = 6.8
    6.8 / 1000 = 0.0068 (good)
    0.141 * 10 = 1.41
    1.41 / 1000 = 0.00141 (good)
    (Could be an issue with the '0')

    I'm using JavaScript to code and Safari to view.

    Any ideas, or should I try the multiply by 10 method?

    EDIT: Have realised this is a common Javascript problem called a rounding error, and it's not just with 0's.
    Last edited by joshy; 11-22-2011 at 02:53 AM. Reason: did some more research
    ♥♦♣♠ Pixel

  2. #2
    hrvat's Avatar
    hrvat is offline x10Hosting Member hrvat is an unknown quantity at this point
    Join Date
    Dec 2010
    Location
    Zagreb
    Posts
    66

    Lightbulb Re: Javascript dividing by 100 giving long decimal numbers

    Are you familiar with web 2.0 and web workers ? Will you try to find response on you question here.

    http://www.ibm.com/developerworks/we...rkers/?ca=drs-
    Last edited by hrvat; 11-22-2011 at 05:24 AM.
    dinomirt96 likes this.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript dividing by 100 giving long decimal numbers

    It's not a JS error, it's a consequence of floating point representation. For the same reason that the decimal expansion of 1/3 doesn't have a finite representation in base 10, numbers such as 1/5 don't have a finite representation in base 2 (which would be 0.00110011...₂). It's just that JS is truncating/rounding to hide the error. Floating point operations can compound this error (how much depends on the operations and arguments), which is likely why you see it with some calculations and not others, even when the resulting value should be the same.

    Code:
    (0.2).toPrecision(17);
    // result is "0.20000000000000001"
    If JS is including non-significant digits when displaying the numbers, you'll have to perform rounding yourself.

    See "What every computer scientist should know about floating-point arithmetic" for the full story.

    @hrvat: how is that relevant?
    Last edited by misson; 11-22-2011 at 07:12 AM.
    essellar and karimirt47 like this.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. UserNames With Numbers
    By cybrax in forum Crossfire
    Replies: 2
    Last Post: 11-05-2011, 06:54 AM
  2. MYSQL DB Numbers
    By cyborg27643852 in forum Free Hosting
    Replies: 1
    Last Post: 07-29-2011, 06:06 PM
  3. Random numbers in PHP
    By cursedpsp in forum Tutorials
    Replies: 8
    Last Post: 11-13-2008, 06:43 AM
  4. 1:2 Ratio /w Doubling Numbers...
    By Jordan K in forum Off Topic
    Replies: 18
    Last Post: 12-22-2005, 04:56 PM
  5. DNS Numbers
    By Chirantha in forum Free Hosting
    Replies: 1
    Last Post: 07-10-2005, 10:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers