- 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
Vones.
I can't unsee that now. It looks like a cross between Jones and Vimes. I think I'm going to have to take a say's sick leave.
Admin
*day's
Admin
My guess is that the original programmer of this came directly from assembler.
Admin
Vformat = ("00000000"+Vnum.toFixed(2)).slice(-11);
Admin
Does anybody really care about pennies any more?
Admin
And the thousands separators?
Admin
FWIW, super-simple one-liner:
return (Math.round(Vnum * 100) / 100).toLocaleString();
Admin
Vformat = Vnum.toFixed(2).replace(/(\d)(?=(\d{3})+.)/g, '$1,');
Admin
The above post just proves that the code in the article is not as terrible as you first thought after all.
Admin
You can't do this replacement. toLocaleString() wouldn't let us e. g. Germans see that beautiful American decimal and thousands separators where they belong. E. g. 1234.56 would be represented as "1.234,56" and not as the JavaScript gods designed it, as "1,234.56".
Admin
Just Superman and Michael Bolton.
Admin
Michael Bolton? The no talent assclown?
Admin
if it's that important to you: return (Math.round(Vnum * 100) / 100).toLocaleString("en-US");
Admin
Wha...? If you want js to read it, leave it as Number type. When you display it to user, use their locale. Number.prototype.toLocaleString() was provided by the JavaScript gods.
Admin
"Parsimony" <-- I see what you did there. Next up: "metaphor," followed by "horticulture"
Admin
When I was but a lad, we had a five-instruction recursive number formatter for the PDP-10. Anything larger than that was considered wasteful and time-consuming. If you ever wondered how these ancient, glacial, tiny systems got anything at all done, this is how.
Admin
Number parsing? That looks more like number formatting to me.
Admin
Ah, no. No. You don't understand. It's, uh, very complicated. It's, uh, it's, it's aggregate so I'm talking about fractions of a cent that, uh, over time, they add up to a lot.
Admin
@DigitalDan Really? The shortest I can find is 8 instructions: http://pdp10.nocrew.org/docs/instruction-set/Calculator.html DECOT1: IDIVI A,10. HRLM B,(P) ;Save remainder in LH of stack word ;whose RH contains our return address. SKIPE A ;If quotient is nonzero, PUSHJ P,DECOT1 ;print higher-order digits of number. HLRZ A,(P) ;Get back this remainder (this digit). ADDI A,"0 .IOT CHTTYO,A POPJ P,
Pseudocode:
sub DECOT1(A) { A, B = divmod(A, 10) returnAddress.left = B.right if (A != 0) { call DECOT1(A) } A = {.left = 0, .right = returnAddress.left} A += '0' put_char(A) return // to returnAddress.righ, ignoring returnAddress.left }
Admin
I miss my DEC toys......(wish I had the space to get them out of storage and back into live usage)...Though I never had a 10 or a 20 (I worked on them, but somehow they never made it to the tertiary market)
Admin
vomits
Admin
I, for one, care about penises. And coffee. I might not have enough coffee.
Also, is "unindentional" already a word for things like the goto fail bug?
Admin
View -> Source is all you need for a Remy posting.
Admin
I know enough German to recognize a naughty nickname when I see it ...
Admin
When I was a wee lad, we didn't have CSS at all. And I don't think I'm that old.
Admin
Some alternatives: Vformat = Vnum.toFixed(2).replace(/\B(?=(...)+$)/g, ','); Vformat = new Intl.NumberFormat("en-US", {style: "currency", currency: "USD"}).format(Vnum).slice(1);
Addendum 2016-04-20 07:09:
Someone already pointed out the lack of thousand separators, but also leading zeros were not actually requested.
Doesn't work for multiples of 10 cents.
Doesn't work for numbers between 10000 and 1000000.
Some alternatives: Vformat = Vnum.toFixed(2).replace(/\B(?=(\d\d\d)+\b)/g, ','); Vformat = Vnum.toFixed(2).replace(/\B(?=...(...)+$)/g, ','); Vformat = new Intl.NumberFormat("en-US", {style: "currency", currency: "USD"}).format( Vnum).slice(1);
Addendum 2016-04-20 07:10: Some alternatives:
Vformat = Vnum.toFixed(2).replace(/\B(?=(...)+$)/g, ',');
Vformat = new Intl.NumberFormat("en-US", {style: "currency", currency: "USD"}).format(Vnum).slice(1);
Oh, for a decent commenting system. Or even Discurse.