• Korrat (unregistered)

    The best part is that this functions works with arbitrary numerical systems, such as hex: DEADBEEF is perfectly valid according to the first check.

  • LZ79LRU (unregistered)

    Since this is frontend Javascript I can't wait to see it run on a system where the locale has its decimal separator set to ','.

  • Sandra from InitAg (unregistered)

    Code so bad that the blog software threw up and ran a strikethrough on the entire thing.

  • DQ (unregistered)

    I don't know much about javascript but if num is something like "frist", won't it just return that?

  • (nodebb) in reply to DQ

    frist is ok because it doesn't have a fractional part.

  • Scragar (unregistered) in reply to LZ79LRU

    Actually not a concern.

    The 262 spec for JavaScript says that Number.prototype.toString and Number.prototype.toFixed should both use . as the decimal separator and no thousands separator.

    If you want a string in the users locale you should use Number.prototype.toLocaleString which takes an optional object with properties for things like style(currency, percent, units), min/max integer/fraction/significant digits, rounding options(ceil, floor, trunc expand, halfCeil, halfFloor, etc), and how to handle trailing zeros(all, never, auto(which keeps them if you use fraction/significant digit specifiers), and stripIfInteger which does what you'd expect).

  • (nodebb)

    JS does already have a function that rounds to a specified number of decimal places: Number.prototype.toFixed. They even use that. The one-liner for the whole thing is parseFloat(num.toFixed(4))

    Addendum 2025-08-12 09:53: Oops, it should be parseFloat(Number(num).toFixed(4))

  • MaxiTB (unregistered)

    I find it funny how often devs seem to jiggle around with string instead of using simple math. I once saw some implementing log10 by counting the string length ...

  • (nodebb) in reply to Korrat

    I am not kidding - my company forbids the use of spelling out anything in hex like DEADBEEF because it may be offensive.

  • Jason Stringify (unregistered) in reply to jeremypnet

    *fristional

  • (nodebb) in reply to Barry Margolin

    Pardon my ignorance of java, but does "To.Fixed" round or floor? And if it rounds, does it use the Banker's Rounding or just shove all numbers ending in "5" up one? this matters a lot if you are doing data anallysis.

  • (nodebb) in reply to cellocgw

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

  • Klimax (unregistered) in reply to TheGreatLobachevsky

    I see we got bonus WTF...

  • (nodebb) in reply to Barry Margolin

    The one-liner for the whole thing is parseFloat(num.toFixed(4))

    Yes, but why use strings at all here for God's sake? *points at the article*

  • Martin Tilsted (unregistered)

    Sorry, but there are NO WAY EVER you should need to round a float/double to a specific number of digits after the . And there is to reasons for this:

    1: It is almost always impossible due to the way floating points are represented. Remember that 0.1+0.2!=0.3 when doing float math.

    2: You don't need it. The only case ever where you care is when you need to show the float as a string, and the correct way, is just use a method which gives you a string representation with the specified number of digits. But this does NOT round the float itself, it creates a new string representation.

    3: If it's because your float is representing an amount of money, then all I can say is NO. NO NO NO NO NO. That is not the way to handle cents. Please google why not to use floats for money if you ever want to do something like this.

  • Zed (unregistered)

    You know what description I really don't want of a programming language? "Frequently surprising". That's quite high in the list of things programming languages shouldn't be.

    Stable, predictable, tested, documented - all good descriptions. "Surprising"? Definitely not.

Leave a comment on “Round Strips”

Log In or post as a guest

Replying to comment #:

« Return to Article