Solved

Formatting Large Numbers with PRINTF

Posted on 2007-11-19
4
539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

635 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