Notice: You are browsing the documentation for PrestaShop 9, which is currently in development.
You might want to read the documentation for the current version, PrestaShop 8. Read the current version of this page
The Locale
component is in charge of formating numbers, prices and percentages.
It is accessible through the Context
It provides useful methods such as formatNumber()
and formatPrice()
.
The Locale
is initialized with :
code
in IETF tag format (en-US
, fr-FR
, …),PrestaShop\PrestaShop\Core\Localization\Specification\Number
NumberSpecification
PrestaShop\PrestaShop\Core\Localization\Specification\NumberCollection
PriceSpecificationMap
PrestaShop\PrestaShop\Core\Localization\Number\Formatter
Number formatter
This method receives one parameter $number
as int
, float
or string
. It returns a formatted number, as a string
, taking the NumberSpecification
used by the locale, set during its initialization.
With a en-US
locale,
$number = 1234.56;
var_dump(Context::getContext()->getCurrentLocale()->formatNumber($number));
Will dump:
string(8) "1,234.56"
This method receives two parameters, the first one $number
as int
, float
or string
, and the second one $currencyCode
as string
. It returns a formatted price, as a string
, taking the matching PriceSpecification
identified by the $currencyCode
.
With a en-US
locale, and a GBP
currency code:
$price = 1234.56;
var_dump(Context::getContext()->getCurrentLocale()->formatPrice($price, 'GBP'));
// string(10) "£1,234.56"
With a en-US
locale, and a EUR
currency code,
$price = 1234.56;
var_dump(Context::getContext()->getCurrentLocale()->formatPrice($price, 'EUR'));
// string(10) "€1,234.56"