Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting numbers with commas 1

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
Is there a function in sprintf to split numbers with commas for readability. I can roll my own but would rather not reinvent the wheel.

ie.
12000000 => 12,000,000
256675 => 256,675
It is purely for readability.

Keith
 
Thanks rharsh - that is amazing.
Code:
sub commify {

local $_  = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}
I must confess - I haven't a clue how it does it but I got what I asked for.
Is there an explanation somewhere or should I just quit while I am ahead?

Keith
 
While there are 4 or more sequential digits in the string, replace them with the first bunch of digits, a comma, and the last 3 digits."

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top