Solved

How to format a number?

Posted on 2001-06-15
1
178 Views
Last Modified: 2010-03-05
I want to format numbers by inserting commas, like 1012345 as 1,012,345 Is there a nice reliable way of doing that?
0
Comment
Question by:Biffo
1 Comment
 
LVL 5

Accepted Solution

by:
marecs earned 50 total points
ID: 6194150
From the Perl FAQ

perldoc -q "How can I output my numbers with commas added?"

sub commify {                                                  
    local $_  = shift;                                          
    1 while s/^([-+]?\d+)(\d{3})/$1,$2/;                        
    return $_;                                                  
}                                                              

$n = 23659019423.2331;                                          
print "GOT: ", commify($n), "\n";                              
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question