Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

Formatting Large Numbers with PRINTF

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
cs76737
Asked:
cs76737
  • 2
1 Solution
 
TalmashCommented:
try this regex:

s/(\d{3})/$1,/g
0
 
cs76737Author Commented:
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
 
TalmashCommented:
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
 
FishMongerCommented:
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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now