JavaScript Rounding Question...

Discussion in 'Mac Help/Tips' started by simX, Aug 19, 2002.

  1. simX macrumors 6502a

    simX

    Joined:
    May 28, 2002
    Location:
    Bay Area, CA
    #1
    I have a JavaScript program that does some rounding of numbers so that they don't get too long when presented in text fields.

    However, it seems that Internet Explorer (conveniently) likes to ignore these things and continues to display the unrounded numbers (for example, it displays "8.019600000000000000001" instead of the "8.0196" that the JavaScript code should create).

    Here's the snippit of code:

    How come that doesn't work in Internet Explorer, but it seems to work fine in Mozilla? (Note: "tempK3" is a previous variable.)
     
  2. cleo macrumors 65816

    cleo

    Joined:
    Jan 21, 2002
    Location:
    Tampa Bay Area, FL, USA
    #2
    I don't know the answer to your specific question or if there's a workaround for IE, but in my experience it's always 1000% safer to use server-side scripting language for such things... there's just too many quirks from browser to browser.
     
  3. simX thread starter macrumors 6502a

    simX

    Joined:
    May 28, 2002
    Location:
    Bay Area, CA
    #3
    Heh, yeah, but what would you suggest in lieu of JavaScript? Basically what I'm trying to do is on-the-fly calculating of the x and y values of the point where a user clicks on the graph. It seems JavaScript is the best way to do this, even if I do have to compensate for some browser quirks.
     
  4. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    Why not use 10000 instead of 1000 in both places, as a workaround?

    That should eliminate the 1, at least, in this case and hopefully, other cases wouldn't happen. :)
     
  5. Choppaface macrumors 65816

    Joined:
    Jan 22, 2002
    Location:
    SFBA
    #5
    isn't there a truncate function?

    you could try just grabbing a substring of the first x chars in the number (or you could split it into an array on the period, replace element 1 of that array (the nums after the decimal) with it's first few digits, then join using ".".......)
     
  6. backspinner macrumors 6502a

    Joined:
    Apr 29, 2002
    Location:
    Eindhoven
    #6
    this snippet handles cases like this, including leading zero and decimal separators:

    if (b.value.charAt(0) == '.') b.value = "0" + b.value;
    if (b.value.indexOf('.') == -1) b.value = b.value + ".00";
    while (b.value.charAt(b.value.length-3) != '.') b.value = b.value + '0';
     
  7. oldMac macrumors 6502a

    Joined:
    Oct 25, 2001
    #7
    Strange Problem = Strange Solution

    Hi SimX,

    Strange issue you've come across.
    The error isn't happening in the round. The error is happening in the division portion of your number. Apparently, IE's division method isn't very precise.

    Oddly, but fortunately, if you convert the number to a string, IE seems to recognize that it's not smart enough to hold a number of that precision and will happily truncate it for you. (This works fine in Netscape 4.7 and 6, too.)

    -------------- snip ----------------

    var tempK3 = 8.019600000000000000001;
    var kval = Math.round(tempK3*10000)/10000;

    document.show.kvalue.value = String(kval);

    --------------- end snip -----------
     
  8. bikebuddy macrumors newbie

    Joined:
    Jul 18, 2002
    #8
    I think OldMac is right. I don't program in JavaScript, but I suspect your problem is in the decimal to binary conversion when dividing by 1000. In particular, while 10^-3 has a finite decimal representation, it does not have a finite binary representation and so the division is not performed exactly.
     

Share This Page