• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

How to format a number?

I want to format numbers by inserting commas, like 1012345 as 1,012,345 Is there a nice reliable way of doing that?
0
Biffo
Asked:
Biffo
1 Solution
 
marecsCommented:
From the Perl FAQ

perldoc -q "How can I output my numbers with commas added?"

sub commify {                                                  
    local $_  = shift;                                          
    1 while s/^([-+]?\d+)(\d{3})/$1,$2/;                        
    return $_;                                                  
}                                                              

$n = 23659019423.2331;                                          
print "GOT: ", commify($n), "\n";                              
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

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