So after all the hullabaloo about commify a few weeks ago, I took a
little while last night to write a plugin/filter wrapper around
Number::Format.  The way it works is:  You USE Number.Format with the
appropriate localizing options, as described in
L<Number::Format/"DESCRIPTION">, and it installs several filters into
the Context.

  [% USE Number.Format %]

attempts to read the local settings from POSIX::locale, which should
probably do the right thing.  You can always explicitly set options:

  [% USE Number.Format(thousands_sep   => '.',
                       decimal_point   => ',',
                       int_curr_symbol => 'DEM') %]

And then the installed filters will use these values.

Template::Plugin::Number::Format installs filters into the Context based
on what Number::Format exports into the caller when you invoke it with:

  use Number::Format ':subs';

Which, as of version 1.44, is: round, format_number, format_negative,
format_picture, format_price, format_bytes, and unformat_number.  Each
filter is dynamic, which means they take the same arguments in your
template as they do in Perl:

  [% USE Number.Format %]
  [% big_num = "123456789.987654321" %]
  [% big_num | format_number %]
  [% big_num | format_number(2) %]
  [% big_num | format_number(8) %]
  [% "123.3" | format_number(4, 4) %]

Yields:

  123,456,789.99
  123,456,789.99
  123,456,789.987654
  123.3000

And, using the German example above:

  [% USE Number.Format(thousands_sep   => '.',
                       decimal_point   => ',',
                       int_curr_symbol => 'DEM') %]
  [% big_num = "123456789.987654321" %] 
  [% big_num | format_number %]
  [% big_num | format_number(2) %]
  [% big_num | format_number(8) %]
  [% "123.3" | format_number(4, 4) %]

Gives us:

  123.456.789,99
  123.456.789,99
  123.456.789,987654
  123,3000

And so on.  The tarball includes tests and a docsrc document that has
documentation, which is mostly lifted from Number::Format, with emphasis
on the mechanics, since the Number::Format perldocs explain the rest.

A tarball is available at
<http://apache-tt2.sourceforge.net/Template-Plugin-Number-Format-1.01.tar.gz>,
and is also attached.

Comments?

(darren)

-- 
There is no expedient to which a man will not go to
avoid the real labour of thinking.

Attachment: Template-Plugin-Number-Format-1.01.tar.gz
Description: application/tar-gz

Reply via email to