number formatting..

J

Jaan Kronberg

Hello there,


my $somenumber = 1000000;

I'd like to format this $somenumber to look like 1.000.000,00
Can it be done with sprintf? Other ways?

Thx,
jk
 
P

Paul Boardman

Jaan Kronberg said:
my $somenumber = 1000000;

I'd like to format this $somenumber to look like 1.000.000,00
Can it be done with sprintf? Other ways?

perldoc -q "numbers with commas"

should get you on your way.

Paul
 
T

Tore Aursand

my $somenumber = 1000000;

I'd like to format this $somenumber to look like 1.000.000,00
Can it be done with sprintf? Other ways?

This is partly a FAQ;

perldoc -q commas

The decimal part is solved by using sprintf, though.
 
G

Gunnar Hjalmarsson

Jaan said:
my $somenumber = 1000000;

I'd like to format this $somenumber to look like 1.000.000,00
Can it be done with sprintf? Other ways?

This is a three step solution:

sub numfmt {
local $_ = shift;
$_ = sprintf '%.2f', $_; # 1. Decimals
tr/./,/; # 2. Comma as dec. point
1 while s/^([-+]?\d+)(\d{3})/$1.$2/; # 3. Points added
return $_;
}
print numfmt($somenumber);

I suppose it's possible to have Perl use a comma as the decimal point
by using the locale pragma, but I didn't succeed when testing it.

As Paul and Tore mentioned, there is a Q/A in the FAQ that provides a
couple of methods for adding commas to numbers. I used one of those
methods, exchanging the comma for a point.
 
D

David

Hello there,


my $somenumber = 1000000;

I'd like to format this $somenumber to look like 1.000.000,00
Can it be done with sprintf? Other ways?

Thx,
jk

If you want a quick way.

use Number::Format;
my $nu = new Number::Format(
-thousands_sep => ',',
-decimal_point => '.'
);

$nu->format_number($somenumber);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top