- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
The best part is that this functions works with arbitrary numerical systems, such as hex: DEADBEEF is perfectly valid according to the first check.
Admin
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 ','.
Admin
Code so bad that the blog software threw up and ran a strikethrough on the entire thing.
Admin
I don't know much about javascript but if num is something like "frist", won't it just return that?
Edit Admin
frist
is ok because it doesn't have a fractional part.Admin
Actually not a concern.
The 262 spec for JavaScript says that
Number.prototype.toString
andNumber.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).Edit Admin
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 isparseFloat(num.toFixed(4))
Addendum 2025-08-12 09:53: Oops, it should be
parseFloat(Number(num).toFixed(4))
Admin
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 ...
Edit Admin
I am not kidding - my company forbids the use of spelling out anything in hex like DEADBEEF because it may be offensive.
Admin
*fristional
Edit Admin
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.
Edit Admin
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
Admin
I see we got bonus WTF...
Edit Admin
Yes, but why use strings at all here for God's sake? *points at the article*
Admin
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.
Admin
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.