SYNOPSIS

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

ABSTRACT

Template::Plugin::Number::Format makes the number-munging grooviness of Number::Format available to your templates. It is used like a plugin, but installs filters into the current context.

DESCRIPTION

All filters created by Template::Plugin::Number::Format can be configured by constructor options and options that can be passed to individual filters. See \*(L"\s-1METHODS\s0\*(R" in Number::Format for all the details.

Constructor Parameters

The \s-1USE\s0 line accepts the following parameters, all optional, which define the default behavior for filters within the current Context:

\s-1THOUSANDS_SEP\s0

character inserted between groups of 3 digits

\s-1DECIMAL_POINT\s0

character separating integer and fractional parts

\s-1MON_THOUSANDS_SEP\s0

like \s-1THOUSANDS_SEP\s0, but used for format_price

\s-1MON_DECIMAL_POINT\s0

like \s-1DECIMAL_POINT\s0, but used for format_price

\s-1INT_CURR_SYMBOL\s0

character(s) denoting currency (see format_price())

\s-1DECIMAL_DIGITS\s0

number of digits to the right of dec point (def 2)

\s-1DECIMAL_FILL\s0

boolean; whether to add zeroes to fill out decimal

\s-1NEG_FORMAT\s0

format to display negative numbers (def -x)

\s-1KILO_SUFFIX\s0

suffix to add when format_bytes formats kilobytes

\s-1MEGA_SUFFIX\s0

suffix to add when format_bytes formats megabytes

\s-1GIGA_SUFFIX\s0

suffix to add when format_bytes formats gigabytes

Using Template::Plugin::Number::Format

When you invoke:

[% USE Number.Format(option = value) %]

the following filters are installed into the current Context:

round($precision)

Rounds the number to the specified precision. If \*(L"$precision\*(R" is omitted, the value of the \*(L"\s-1DECIMAL_DIGITS\s0\*(R" parameter is used (default value 2). Formats a number by adding \*(L"\s-1THOUSANDS_SEP\s0\*(R" between each set of 3 digits to the left of the decimal point, substituting \*(L"\s-1DECIMAL_POINT\s0\*(R" for the decimal point, and rounding to the specified precision using \*(L"round()\*(R". Note that \*(L"$precision\*(R" is a maximum precision specifier; trailing zeroes will only appear in the output if \*(L"$trailing_zeroes\*(R" is provided, or the parameter \*(L"\s-1DECIMAL_FILL\s0\*(R" is set, with a value that is true (not zero, undef, or the empty string). If \*(L"$precision\*(R" is omitted, the value of the \*(L"\s-1DECIMAL_DIGITS\s0\*(R" parameter (default value of 2) is used.

format_negative($picture)

Formats a negative number. Picture should be a string that contains the letter \*(L"x\*(R" where the number should be inserted. For example, for standard negative numbers you might use \*(L"-x\*(R", while for accounting purposes you might use \*(L"(x)\*(R". If the specified number begins with a - character, that will be removed before formatting, but formatting will occur whether or not the number is negative.

format_picture($picture)

Returns a string based on \*(L"$picture\*(R" with the \*(L"#\*(R" characters replaced by digits from \*(L"$number\*(R". If the length of the integer part of $number is too large to fit, the \*(L"#\*(R" characters are replaced with asterisks (\*(L"*\*(R") instead.

format_price($precision)

Returns a string containing \*(L"$number\*(R" formatted similarly to \*(L"format_number()\*(R", except that the decimal portion may have trailing zeroes added to make it be exactly \*(L"$precision\*(R" characters long, and the currency string will be prefixed. If the \*(L"\s-1INT_CURR_SYMBOL\s0\*(R" attribute of the object is the empty string, no currency will be added. If \*(L"$precision\*(R" is not provided, the default of 2 will be used.

format_bytes($precision)

Returns a string containing \*(L"$number\*(R" formatted similarly to \*(L"format_number()\*(R", except that if the number is over 1024, it will be divided by 1024 and the value of \s-1KILO_SUFFIX\s0 appended to the end; or if it is over 1048576 (1024*1024), it will be divided by 1048576 and \s-1MEGA_SUFFIX\s0 appended to the end. Negative values will result in an error. If \*(L"$precision\*(R" is not provided, the default of 2 will be used.

unformat_number

Converts a string as returned by \*(L"format_number()\*(R", \*(L"format_price()\*(R", or \*(L"format_picture()\*(R", and returns the corresponding value as a numeric scalar. Returns \*(L"undef\*(R" if the number does not contain any digits.

RELATED TO Template::Plugin::Number::Format…

Template, Number::Format

AUTHOR

darren chamberlain <[email protected]>