Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: "Commifying" a number

by MarkM (Curate)
on Dec 13, 2002 at 06:49 UTC ( [id://219518]=note: print w/replies, xml ) Need Help??


in reply to "Commifying" a number

I decided to take up your challenge. Create a single regular expression to 'commify' a number. Here is my entry:

    $number =~ s/(\d+?)(?=(?:\d{3})+(?:\.|\z))/$1,/g;

Can anybody spot a case where this would not work?

Since the regular expression is looping in C code, rather than Perl code, I expect this solution to execute faster.

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

    Sorry, but yes. It doesn't handle decimal places. Works well on integers though.

    C:\test>commify -0.0001234567890 commified becomes -0.000,123,456,789 -0.0012345678900 commified becomes -0.00,123,456,789 -0.0123456789000 commified becomes -0.0,123,456,789 -0.1234567890000 commified becomes -0.123,456,789 -1.2345678900000 commified becomes -1.23,456,789 -12.3456789000000 commified becomes -12.3,456,789 -123.4567890000000 commified becomes -123.456,789 -1234.5678900000000 commified becomes -1,234.56,789 -12345.6789000000010 commified becomes -12,345.6,789 -123456.7890000000000 commified becomes -123,456.789 -1234567.8899999999000 commified becomes -1,234,567.89 -12345678.9000000000000 commified becomes -12,345,678.9 -123456789.0000000000000 commified becomes -123,456,789 -1234567890.0000000000000 commified becomes -1,234,567,890 -12345678900.0000000000000 commified becomes -12,345,678,900 -123456789000.0000000000000 commified becomes -123,456,789,000 -1234567890000.0000000000000 commified becomes -1,234,567,890,000 -12345678900000.0000000000000 commified becomes -12,345,678,900,000 -123456789000000.0000000000000 commified becomes -123,456,789,000,000 -1234567890000000.0000000000000 commified becomes -1.23456789e+015 C:\test>

    Examine what is said, not who speaks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found