For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Formatting Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Formatting Question
Sanbuah@aol.com

2006-01-10, 4:02 am

Hello,

Here's my question.

Let's say I have the following numbers and I want to print them out so they
are formatted in money terms:

examples:

10834.00
1939432.00


to print out as:

$10,834.00
$1,939,432.00

How can I do this? I was suspecting that the "printf" or "sprintf" functions
may accomplish this. But I was not able to figure it out. I was thinking
that I may have to build some kind of subroutine to accomplish this but I was
hoping there would simpler way. Can anyone help me?

Thanks in advance.

Jeremy Vinding

2006-01-10, 4:02 am

Sanbuah@aol.com wrote:
> Hello,
> =20
> Here's my question.
> =20
> Let's say I have the following numbers and I want to print them out so =

they =20
> are formatted in money terms:
> =20
> examples:
> =20
> 10834.00
> 1939432.00
> =20
> =20
> to print out as:
> =20
> $10,834.00
> $1,939,432.00
> =20
> How can I do this? I was suspecting that the "printf" or "sprintf" fun=

ctions=20
> may accomplish this. But I was not able to figure it out. I was thin=

king=20
> that I may have to build some kind of subroutine to accomplish this bu=

t I was=20
> hoping there would simpler way. Can anyone help me?
> =20
> Thanks in advance.
>=20


$ perldoc -q "numbers with commas"

How can I output my numbers with commas added?

This subroutine will add commas to your number:

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

This regex from Benjamin Goldberg will add commas to numbers:

s/(^[-+]?\d+?(?=3D(?>(?:\d{3})+)(?!\d))=E2=94=82\G\d{3}(?=3D\d=
))/$1,/g;

....

--jjv
usenet@DavidFilmer.com

2006-01-10, 4:02 am

Sanbuah@aol.com wrote:
> Let's say I have the following numbers and I want to print them out so they
> are formatted in money terms:


You can let Number::Format do all the work; for example:

#!/usr/bin/perl
use Number::Format;

my $formatter = new Number::Format (int_curr_symbol => '$');

for (qw/10834.00 1939432.00 12345 9876.543/) {
print $formatter -> format_price($_), "\n";
}
__END__

However, that will put a space between the dollar sign and the value.
If you don't want that, you can do a little more work yourself; change
the "print" statement thus:

print '$', $formatter -> format_number($_, 2, 2), "\n";

--
http://DavidFilmer.com

Tom Phoenix

2006-01-10, 4:02 am

On 1/4/06, Sanbuah@aol.com <Sanbuah@aol.com> wrote:
> Let's say I have the following numbers and I want to print them out so th=

ey
> are formatted in money terms:


Have you seen this sub? It's from p. 184 of the llama book (Learning
Perl, 4th ed.).

sub big_money {
my $number =3D sprintf "%.2f", shift @_;
# Add one comma each time through the do-nothing loop
1 while $number =3D~ s/^(-?\d+)(\d\d\d)/$1,$2/;
# Put the dollar sign in the right place
$number =3D~ s/^(-?)/$1\$/;
$number;
}

That turns 12345678.9 into "$12,345,678.90". Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
usenet@DavidFilmer.com

2006-01-10, 4:02 am

Tom Phoenix wrote:

> Have you seen this sub? It's from p. 184 of the llama book (Learning
> Perl, 4th ed.).


I wonder if it's OK with the book's authors to reprint that? err... I
guess so ;)

--
http://DavidFilmer.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com