Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > number formatting..

Reply
Thread Tools

number formatting..

 
 
Jaan Kronberg
Guest
Posts: n/a
 
      11-27-2003
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


 
Reply With Quote
 
 
 
 
Paul Boardman
Guest
Posts: n/a
 
      11-27-2003

"Jaan Kronberg" <(E-Mail Removed)> wrote in message
news:3fc5f7fb$0$155$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      11-27-2003
On Thu, 27 Nov 2003 15:11:25 +0200, Jaan Kronberg wrote:
> 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.


--
Tore Aursand <(E-Mail Removed)>
"A teacher is never a giver of truth - he is a guide, a pointer to the
truth that each student must find for himself. A good teacher is
merely a catalyst." -- Bruce Lee
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-27-2003
Jaan Kronberg wrote:
>
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
David
Guest
Posts: n/a
 
      12-01-2003
On Thu, 27 Nov 2003 15:11:25 +0200, "Jaan Kronberg"
<(E-Mail Removed)> wrote:

>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);


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
The number name 'System.Web.UI.WebControls' contains more than the maximum number of prefixes. The maximum is 3. mayur ASP .Net 2 07-02-2004 10:35 AM
real number to 16 bit signed number hari VHDL 6 05-02-2004 04:10 PM
IE 6.0 sockets number (TCP/IP channels number) for the same Site ??? taras ASP .Net 1 04-17-2004 04:51 AM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments