Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Answer: How do I add commas to a number?

by Biker (Priest)
on Dec 12, 2001 at 20:49 UTC ( #131290=note: print w/ replies, xml ) Need Help??


in reply to Re: Answer: How do I add commas to a number?
in thread numbers

What happens if your value is 1_000_000 ?

"Livet är hårt" sa bonden.
"Grymt" sa grisen...


Comment on Re: Re: Answer: How do I add commas to a number?
hawk@rthawk.com
by Anonymous Monk on Apr 04, 2002 at 15:16 UTC
    Then simply extend the search/replace pattern. Since this is just cosmetics, it is not detrimental if it fails. Here is a complete example. Run this code and it will become clear what is going on:
    $c1=$c2=$c3=111222333444555; $c1=~ s/^(-?\d+)(\d{3})(\d{3})/$1,$2,$3/; $c2=~ s/^(-?\d+)(\d{3})(\d{3})(\d{3})/$1,$2,$3,$4/; $c3=~ s/^(-?\d+)(\d{3})(\d{3})(\d{3})(\d{3})/$1,$2,$3,$4,$5/; print<<END; $c1 $c2 $c3 END

      It doesn't automatically scale. You must write that code with the knowledge of what is the biggest number you will handle.

      "Since this is just cosmetics, it is not detrimental if it fails."

      My users beg to disagree. Numbers not formatted correctly when presented on our Intranet Web pages may have a very high economical impact, including failed trades, penalty fees and loss of reputation.

      The right answer is here.

        What do you mean "Right answer"? There are many ways of doing things in perl. You don't have get snotty about it. Here is yet another way that is even more efficent then your "right answer", but not as efficent as my previous example:
        &commify($x);#Robust - Does all size numbers sub commify { local $_ = shift; 1 while s/(-?\d+)(\d{3})/$1,$2/; return $_; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-08-28 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (255 votes), past polls