• Quite (unregistered)

    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.

  • Quite (unregistered)

    *day's

  • Syntaxerror (unregistered)

    My guess is that the original programmer of this came directly from assembler.

  • One Liner (unregistered)

    Vformat = ("00000000"+Vnum.toFixed(2)).slice(-11);

  • snoofle (unregistered)

    Does anybody really care about pennies any more?

  • Andy F (unregistered) in reply to One Liner

    And the thousands separators?

  • Andy F (unregistered)

    FWIW, super-simple one-liner:

    return (Math.round(Vnum * 100) / 100).toLocaleString();

  • Better one liner (unregistered)

    Vformat = Vnum.toFixed(2).replace(/(\d)(?=(\d{3})+.)/g, '$1,');

  • v (unregistered) in reply to Better one liner

    The above post just proves that the code in the article is not as terrible as you first thought after all.

  • PWolff (nodebb) in reply to Andy F

    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".

  • operagost (unregistered) in reply to snoofle

    Just Superman and Michael Bolton.

  • Sean Ennis (google) in reply to operagost

    Michael Bolton? The no talent assclown?

  • Plloi (unregistered) in reply to PWolff

    if it's that important to you: return (Math.round(Vnum * 100) / 100).toLocaleString("en-US");

  • Paul Neumann (unregistered) in reply to PWolff

    E. g. 1234.56 would be represented as "1.234,56" and not as the JavaScript gods designed it, as "1,234.56"

    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.

  • Carl Witthoft (google)

    "Parsimony" <-- I see what you did there. Next up: "metaphor," followed by "horticulture"

  • DigitalDan (unregistered)

    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.

  • Richard (unregistered)

    Number parsing? That looks more like number formatting to me.

  • Patrick (unregistered)

    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.

  • Simon Clarkstone (unregistered) in reply to DigitalDan
    Comment held for moderation.
  • TheCPUWizard (unregistered) in reply to Quite

    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)

  • Joseph Osako (google)

    vomits

  • löchlein deluxe (unregistered) in reply to snoofle

    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?

  • Ron Fox (google)

    View -> Source is all you need for a Remy posting.

  • YellowOnline (nodebb) in reply to löchlein deluxe

    I know enough German to recognize a naughty nickname when I see it ...

  • Austin (unregistered)

    When I was a wee lad, we didn't have CSS at all. And I don't think I'm that old.

  • urkerab (nodebb)

    Vformat = ("00000000"+Vnum.toFixed(2)).slice(-11); Someone already pointed out the lack of thousand separators, but also leading zeros were not actually requested. return (Math.round(Vnum * 100) / 100).toLocaleString(); Doesn't work for multiples of 10 cents. Vformat = Vnum.toFixed(2).replace(/(\d)(?=(\d{3})+.)/g, '$1,'); Doesn't work for numbers between 10000 and 1000000.

    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:

    Vformat = ("00000000"+Vnum.toFixed(2)).slice(-11);

    Someone already pointed out the lack of thousand separators, but also leading zeros were not actually requested.

    return (Math.round(Vnum * 100) / 100).toLocaleString();

    Doesn't work for multiples of 10 cents.

    Vformat = Vnum.toFixed(2).replace(/(\d)(?=(\d{3})+.)/g, '$1,');

    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.

Leave a comment on “Parsimony”

Log In or post as a guest

Replying to comment #:

« Return to Article