Solved

Formatting Large Numbers with PRINTF

Posted on 2007-11-19
4
536 Views
Last Modified: 2012-08-14
What is the syntax for PRINTF that I need to use for getting commas in large numbers?  

printf(%.2f, 1000000000)

How do I get it to display   "1,000,000,000"  ?

thanks.
0
Comment
Question by:cs76737
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Talmash
ID: 20312788
try this regex:

s/(\d{3})/$1,/g
0
 

Author Comment

by:cs76737
ID: 20312881
I appreciate the suggestion.  Given how I am using this program, however, I need to get it done within the PRINTF function.  

Is it not possible to do it using PRINTF?
0
 
LVL 6

Expert Comment

by:Talmash
ID: 20312933
the spaces in the left and in the right are functions of your format range and the string length.

you can use substr instead, but need to considere both string & range sizes.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 20313770
No, it's not possible to do by using sprintf by itself, you need to use a regex.
my $number = 1000000000;
$number =~ s/(\d)(?=(\d\d\d)+(?!\d))/$1,/g
print $number;

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

821 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