Solved

Formatting Large Numbers with PRINTF

Posted on 2007-11-19
4
533 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
Comment Utility
try this regex:

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

Author Comment

by:cs76737
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

9 Experts available now in Live!

Get 1:1 Help Now