Re: [Templates] Putting commas in money values
[prev]
[thread]
[next]
[Date index for 2004/08/23]
Not sure if this might help, but here is a perl function I call from a
template to format money. It takes the amount and a flag which specifies
whether to include the dollar sign or not.
sub money {
my $self = shift;
my $number = sprintf "%.2f", shift;
my $nosign = shift;
# Add one comma each time through the do-nothing loop
1 while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/;
# Put the dollar sign in the right place
unless ($nosign == 1) {
$number =~ s/^(-?)/$1\$/;
}
return $number;
}
----- Original Message -----
From: "Buddy Burden" <buddy@xxxxxxxxx.xxx>
To: <templates@xxxxxxxxxxxxxxxx.xxx>
Sent: Thursday, August 19, 2004 2:16 PM
Subject: [Templates] Putting commas in money values
> Guys,
>
> Okay, the canonical MACRO example goes something like:
>
> [% MACRO commify(n) GET n.chunk(-3).join(',') %]
>
> Simple enough. But what if you have decimal places on the end? I mean,
> I know I can do this the hard way, but I keep feeling like I'm missing
> something. Is there some easy way, or maybe existing
> plugin/filter/predefined macro somewhere? I'm sure it's a problem
> others have faced before me.
>
> TIA.
>
>
> -- Buddy
>
> _______________________________________________
> templates mailing list
> templates@xxxxxxxxxxxxxxxx.xxx
> http://lists.template-toolkit.org/mailman/listinfo/templates
>
_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates
 |
 |
Re: [Templates] Putting commas in money values
Chris Ochs 16:24 on 23 Aug 2004
|