Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting Large Numbers with PRINTF

Posted on 2007-11-19
4
Medium Priority
?
540 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

704 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