Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: "Commifying" a number

by sauoq (Abbot)
on Dec 12, 2002 at 15:48 UTC ( [id://219347]=note: print w/replies, xml ) Need Help??


in reply to "Commifying" a number

Another way to do it... (but only works on newer perls with lookbehind.) I didn't realize it was a FAQ when I needed to do it. The theme is the same though. Repeatedly make substitutions until there are none left to make.

1 while ( s/(?<!\b)(\d{3})(?:,|$)/,$1/ ); # Explained 1 while ( # Repeat until no more matches are made. s/(?<!\b)(\d{3}) # Match any three digits not preceded by a word +break. (?:,|$) # That come immediately before a comma or the en +d. /,$1/x # And insert a comma before them. );
-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: "Commifying" a number
by BrowserUk (Patriarch) on Dec 12, 2002 at 19:27 UTC

    Ahem:). This doesn't appear work correctly on numbers with decimal places?


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      sauoq sighs.

      True. It only works on integers. I should have said so.

      How about this...

      s/(\d+)/$_=$1;1while(s|(?<!\b)(\d{3})(?:\b)|,$1|);$_/e

      :-P

      -sauoq
      "My two cents aren't worth a dime.";
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://219347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found