Warning: You are browsing the documentation for PrestaShop 8, which is outdated.
You might want to read an updated version of this page for the current version, PrestaShop 9. Read the updated 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 NumberSpecificationPrestaShop\PrestaShop\Core\Localization\Specification\NumberCollection PriceSpecificationMapPrestaShop\PrestaShop\Core\Localization\Number\Formatter Number formatterThis 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"