Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Mar 2001
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rounding to 8ths in JavaScript

    Does anyone know of a simple way to round a number to the nearest eighth in JavaScript. I have a script that calculates a size, but I need the answer to be delivered to the nearest eighth (.125, .25, etc) I know I could do it with IF statements, but I'm looking for something shorter, if possible (like the CEILING function in Excel).

    TIA,
    Gary

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Rounding to 8ths in JavaScript

    See if this works:

    Math.round(yourVar * 8) / 8

    BTW, the CEILING function in Excel appears to always round up. The above will round to the nearest 0.125, up or down.

  3. #3
    Lounger
    Join Date
    Mar 2001
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rounding to 8ths in JavaScript

    That did it! Thanks!
    Sometimes I just want to kick myself for not seeing the obvious.

    Thanks again,
    Gary

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Rounding to 8ths in JavaScript

    If it were obvious, it wouldn't have taken me an hour to figure out. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

  5. #5
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rounding to 8ths in JavaScript

    2 weeks after you posted- nice reply.

    Thanks Jeff for a late night laugh.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Posting Permissions

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