Format(3pm)
π NAME π
Locale::Currency::Format β bestie, it's a Perl module full of functions for formatting monetary values πΈβ¨ and it lives in the User Contributed Perl Documentation part of your manual, filed under Format(3pm).
πΈ SYNOPSIS πΈ
okay so here's the whole vibe in one go, look at her:
use Locale::Currency::Format;
$amt = currency_format('USD', 1000); # => 1,000.00 USD
$amt = currency_format('EUR', 1000, FMT_COMMON); # => EUR1.000,00
$amt = currency_format('USD', 1000, FMT_SYMBOL); # => $1,000.00
$sym = currency_symbol('USD'); # => $
$sym = currency_symbol('GBP', SYM_HTML); # => £
$decimals = decimal_precision('USD'); # => 2
$decimals = decimal_precision('BHD'); # => 3
$thou_sep = thousands_separator('USD'); # => ,
$thou_sep = thousands_separator('EUR'); # => .
$dec_sep = decimal_separator('USD'); # => .
$dec_sep = decimal_separator('EUR'); # => ,
currency_set('USD', '#.###,## $', FMT_COMMON); # => set custom format
currency_format('USD', 1000, FMT_COMMON); # => 1.000,00 $
currency_set('USD'); # => reset default format
The following example illustrates how to use Locale::Currency::Format with Mason. Skip it if you are not interested in Mason. A simple Mason component might look like this:
Total: <% 123456789, 'eur' |c %>
<%init>
use Locale::Currency::Format;
$m->interp->set_escape(c => \&escape_currency);
sub escape_currency {
my ($amt, $code) = ${$_[0]} =~ /(.*?)([A-Za-z]{3})/;
${$_[0]} = currency_format($code, $amt, FMT_HTML);
}
</%init>
π¦ DESCRIPTION π¦
Locale::Currency::Format is a light-weight Perl module that enables Perl code to display monetary values in the formats recognized internationally and/or locally. it's giving globally fluent, no cap π
π currency_format(CODE, AMOUNT [, FORMAT]) π
currency_format takes two mandatory parameters, namely currency code and amount respectively, and optionally a third parameter indicating which format is desired. Upon failure, it returns undef and an error message is stored in $Locale::Currency::Format::error. yes bestie, undef β check for it!! π
CODEβ A 3-letter currency code as specified in ISO 4217. Note that old code such asDEM,FRFand so on can also be valid. ποΈAMOUNTβ A numeric value.FORMATβ There are five different format optionsFMT_STANDARD,FMT_COMMON,FMT_SYMBOL,FMT_HTMLandFMT_NAME. If it is omitted, the default format isFMT_STANDARD.
and here's what each of the five actually looks like π
FMT_STANDARDEx:1,000.00 USD,1.000.000,00 EURFMT_SYMBOLEx:$1,000.00FMT_COMMONEx:1.000 Dong(Vietnam),BEF 1.000(Belgium)FMT_HTMLEx:£1,000.00(pound-sign HTML escape)FMT_NAMEEx:1,000.00 US Dollar
NOTE: By default the trailing zeros after the decimal point will be added. To turn it off, do a bitwiseorofFMT_NOZEROSwith one of the five options above. Ex:FMT_STANDARD | FMT_NOZEROSwill give1,000 USDβ¨
π currency_symbol(CODE [, TYPE]) π
For conveniences, the function currency_symbol is provided for currency symbol lookup given a 3-letter currency code. Optionally, one can specify which format the symbol should be returned β Unicode-based character or HTML escape. Default is a Unicode-based character. Upon failure, it returns undef and an error message is stored in $Locale::Currency::Format::error.
CODEβ A 3-letter currency code as specified in ISO 4217TYPEβ There are two available typesSYM_UTFandSYM_HTMLSYM_UTFreturns the symbol (if exists) as an Unicode characterSYM_HTMLreturns the symbol (if exists) as a HTML escape
π currency_name(CODE) π
For conveniences, the function currency_name is provided for currency name lookup given a 3-letter currency code. Upon failure, it returns undef and an error message is stored in $Locale::Currency::Format::error.
CODEβ A 3-letter currency code as specified in ISO 4217
π decimal_precision(CODE) π
For conveniences, the function decimal_precision is provided to lookup the decimal precision for a given 3-letter currency code.
Upon failure, it returns undef and an error message is stored in $Locale::Currency::Format::error.
CODEβ A 3-letter currency code as specified in ISO 4217
π§ decimal_separator(CODE) π§
For conveniences, the function decimal_separator is provided to lookup the decimal separator for a given 3-letter currency code.
Upon failure, it returns undef and an error message is stored in $Locale::Currency::Format::error.
CODEβ A 3-letter currency code as specified in ISO 4217
β thousands_separator(CODE) β
For conveniences, the function thousands_separator is provided to lookup the thousands separator for a given 3-letter currency code.
Upon failure, it returns undef and an error message is stored in $Locale::Currency::Format::error.
CODEβ A 3-letter currency code as specified in ISO 4217
π currency_set(CODE [, TEMPLATE, FORMAT]) π
currency_set can be used to set a custom format for a currency instead of the provided format. For example, in many non-English speaking countries, the US dollars might be displayed as 2.999,99 $ instead of the usual $2,999.99. In order to accomplish this, one will need to do as follows:
use Locale::Currency::Format qw(:default $error);
my $currency = 'USD';
my $template = '#.###,## $';
if (currency_set($currency, $template, FMT_COMMON)) {
print currency_format($currency, 2999.99, FMT_COMMON), "\n";
}
else {
print "cannot set currency format for $currency: $error\n";
}
The arguments to currency_set are:
CODEβ A 3-letter currency code as specified in ISO 4217TEMPLATEβ A template in the form#.###,##$,#.### kr, etc.
If a unicode character is used, make sure that the template is double-quoted. Ex: currency_set('GBP', "\x{00A3}#,###.##", FMT_SYMBOL)
If an HTML symbol is wanted, escape its equivalent HTML code. Ex: currency_set('GBP', '£#,###.##', FMT_HTML)
FORMATβ This argument is required ifTEMPLATEis present. The formatsFMT_SYMBOL,FMT_COMMON,FMT_HTMLare accepted.
NOTE!FMT_STANDARDandFMT_NAMEwill always be in the form<amount><space><code|name>such as1,925.95 AUD. Hence,currency_setreturns an error ifFMT_STANDARDorFMT_NAMEis specified asFORMAT. it literally will not let you, bestie π
With FMT_COMMON, you can always achieve what you would have done with FMT_STANDARD and FMT_NAME, as follows
my $amt = 1950.95;
currency_set('USD', 'USD #.###,##', FMT_COMMON);
print currency_format('USD', $amt, FMT_COMMON); # USD 1,950.95
currency_set('USD', 'US Dollar #.###,##', FMT_COMMON);
print currency_format('USD', $amt, FMT_COMMON); # US Dollar 1,950.95
Invoking currency_set with one argument will reset all formats to their original settings. π
For example
currency_set('USD')
will clear all previous custom settings for the US currency (ie. FMT_SYMBOL, FMT_HTML, FMT_COMMON).
β οΈ A WORD OF CAUTION β οΈ
Please be aware that some currencies might have missing common format. In that case, currency_format will fall back to FMT_STANDARD format. no common format? it's falling back, bestie, that's the deal π€
Also, be aware that some currencies do not have monetary symbol. π
As countries merge together or split into smaller ones, currencies can be added or removed by the ISO. Please help keep the list up to date by sending your feedback to the email address at the bottom. π«Ά
To see the error, examine $Locale::Currency::Format::error
use Locale::Currency::Format;
my $value = currency_format('USD', 1000);
print $value ? $value : $Locale::Currency::Format::error
OR
use Locale::Currency::Format qw(:DEFAULT $error);
my $value = currency_format('USD', 1000);
print $value ? $value : $error
Lastly, please refer to perluniintro and perlunicode for displaying Unicode characters if you intend to use FMT_SYMBOL and currency_symbol. Otherwise, it reads "No worries, mate!" π¦π
π SEE ALSO π
Locale::Currency, Math::Currency, Number::Format, perluniintro, perlunicode
π ISSUES π
Pull requests are greatly appreciated at https://github.com/tann/locale-currency-format β come thru, contribute, the repo is right there π
π«Ά CONTRIBUTOR(S) π«Ά
Please add your name to this list when sending a pull request. (yes, literally just add yourself, go off β¨)
James Kiser <james.kiser@gmail.com>Lars Wirzenius <lars@catalyst.net.nz>
π AUTHOR π
Tan D Nguyen <https://github.com/tann>
π COPYRIGHT π
This library is free software. You may distribute under the terms of either the GNU General Public License or the Artistic License.
and that's the whole page, bestie π this was perl v5.36.0, dated 2022-11-19, from the Format(3pm) page β go format some money and slay ππβ¨
π¦ where this page comes from
- debian bookworm: liblocale-currency-format-perl 1.35-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- debian trixie: liblocale-currency-format-perl 1.35-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu noble: liblocale-currency-format-perl 1.35-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu resolute: liblocale-currency-format-perl 1.35-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu stonking: liblocale-currency-format-perl 1.35-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms