Solved

How to format a number?

Posted on 2001-06-15
1
159 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now