in reply to Re^2: formatting numbers
in thread formatting numbers

Do the formatting in a sub:

use warnings; use strict; print "Current Account Balance: ", asDollars ($_), "\n" for 100000000, 100, 1000; sub asDollars { my $value = reverse sprintf '$%.2f', shift; $value =~ s/(\d{3})(?=\d)/$1,/g; return scalar reverse $value; }

Prints:

Current Account Balance: $100,000,000.00 Current Account Balance: $100.00 Current Account Balance: $1,000.00

Note that scalar is required in the return statement because reverse may otherwise see a list context.


Perl is environmentally friendly - it saves trees