I'm using Number::Format to format values in a template as in the docs:

[% USE Number.Format %]
[% num | format_number %]


Number::Format will use POSIX to determine the current locale -- as does
Perl.    What appears to be happening is "num" above is getting rendered as
a string causing a "isn't numeric" warning in Number::Format.  (See below)


My question is should the filter get the stringified version of the number?
  I tried the (num + 0) trick but that didn't work.


One fix is this:

[%

USE formatter = Number.format;
formatter.format_number( num, 2, 2 );

%]


The problem is I'm using Number::Format widely and using the filter form,
so by calling setlocale() I'll be generating a lot of warnings -- and
getting invalid results.


Here's output from the script below.  Note how the .79 got dropped in the
value formatted within the template.


------------- [fr_FR] ------

Rendered in Perl.
    Number is [123456,789] and formatted is [123456,79]

 -- calling ->process

Argument "123456,789" isn't numeric in numeric comparison (<=>) at
/Users/bill/perl5/perlbrew/perls/perl-5.16.1/lib/site_perl/5.16.1/Number/Format.pm
line 541.


Rendered in Template:
    Lang is 'fr_FR' and fetched locale 'fr_FR'
    Number is '123456,789' => '123456


And the script:


use strict;
use warnings;
use Template;
use POSIX;
use Number::Format;

my $template = <<'EOF';
[% USE Number.Format %]

Rendered in Template:
    Lang is '[% cur_lang %]' and fetched locale '[% locale %]'
    Number is '[% num %]' => '[% num | format_number %]

EOF

my $tt = Template->new;

for my $cur_lang ( qw/ en_US de_DE fr_FR ru_RU en_GB / ) {

    my $num = 123456.789;


    my $locale = setlocale( &LC_ALL, $cur_lang );
    my $formatter = Number::Format->new;
    my $num_formatted = $formatter->format_number( $num );


    warn "\n------------- [$cur_lang] ------\n\n";
    warn "Rendered in Perl.\n    Number is [$num] and formatted is
[$num_formatted]\n";
    warn "\n -- calling ->process \n\n";


    my %stash = (
        cur_lang => $cur_lang,
        locale   => $locale,
        num      => $num,
    );

    $tt->process( \$template, \%stash )
        || die $tt->error;
}







-- 
Bill Moseley
mose...@hank.org
_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to