in reply to Template Toolkit Formatting Data Question (sprintf and commafy)

I'm a bit confused as to what your problem is. Did you try something like this?

use strict; use warnings; use Template; my $tt = Template->new({ FILTERS => { commify => \&commify, }, }); $tt->process(\*DATA, { number => 420000.2527 }) || die $tt->error(); # Perl Cookbook recipe 2.17 sub commify { my $text = reverse $_[0]; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text; } __DATA__ [% number | format('%.02f') | commify %]
This command line script simply creates a custom filter to commify, and uses the built in format filter to truncate the extraneous decimal numbers. This way, you can use number as it is, or chain it to get a combination of commify and sprintf.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
  • Comment on Re: Template Toolkit Formatting Data Question (sprintf and commafy)
  • Download Code

Replies are listed 'Best First'.
Re^2: Template Toolkit Formatting Data Question (sprintf and commafy)
by Tyraziel (Novice) on Jul 13, 2005 at 15:33 UTC

    jeffa,

    Thank you for your help. Your solution works great!

Re^2: Template Toolkit Formatting Data Question (sprintf and commafy)
by Anonymous Monk on Dec 09, 2011 at 08:25 UTC
    I opted to see if I could expand the MACRO provided in the POD - worked well enough for my need but ymmv:
    [%- MACRO commify(n) GET n.replace('\.\d+$','').chunk(-3).join(',') _ +n.replace('^\d+', '') %]