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

Answer: How do I add commas to a number?

by vroom (His Eminence)
on Jan 18, 2000 at 22:58 UTC ( [id://2146]=note: print w/replies, xml ) Need Help??


in reply to How do I add commas to a number?

sub addcommas{ my ($text)=@_; $text=reverse $text; $text=~s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; $text=scalar reverse $text; return $text; }

Replies are listed 'Best First'.
Re: Answer: How do I add commas to a number?
by Anonymous Monk on Dec 12, 2001 at 20:39 UTC
    Very inefficient! Use:
    $commas=~ s/^(-?\d+)(\d{3})/$1,$2/;
    One line does it all.

    hawk@rthawk.com

      What happens if your value is 1_000_000 ?

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

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2024-04-19 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found