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.
cs76737Asked:
Who is Participating?
 
FishMongerConnect With a Mentor Commented:
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
 
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
All Courses

From novice to tech pro — start learning today.