in reply to Re: Re: "Commifying" a number
in thread "Commifying" a number
`perldoc -q comma' yields
How to RTFM is a wonderful guide which will introduce you to various perl resources.Found in C:\Perl\lib\pod\perlfaq5.pod How can I output my numbers with commas added? This one will do it for you: sub commify { local $_ = shift; 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; return $_; } $n = 23659019423.2331; print "GOT: ", commify($n), "\n"; GOT: 23,659,019,423.2331 You can't just: s/^([-+]?\d+)(\d{3})/$1,$2/g; because you have to put the comma in and then recalculate +your position. Alternatively, this code commifies all numbers in a line regardless of whether they have decimal portions, are prec +eded by + or -, or whatever: # from Andrew Johnson <ajohnson@gpu.srv.ualberta.ca> sub commify { my $input = shift; $input = reverse $input; $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g; return scalar reverse $input; }
|
MJD says you can't just make shit up and expect the computer to know what you mean, retardo! ** The Third rule of perl club is a statement of fact: pod is sexy. |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: "Commifying" a number
by Tanalis (Curate) on Dec 12, 2002 at 14:54 UTC | |
by PodMaster (Abbot) on Dec 12, 2002 at 15:12 UTC |