I’m attempting to achieve the same thing I can achieve in javascript like so:
Intl.NumberFormat('en-CA', {style: 'currency', currency: 'CAD', notation: 'compact', maximumSignificantDigits: 5 }).format(12345000000);
// Output: $12.345B"
Intl.NumberFormat('fr-CA', {style: 'currency', currency: 'CAD', notation: 'compact', maximumSignificantDigits: 5 }).format(12345000000);
// Output: "12,345 G$"
I can do the shortform notation like so:
$formatter = new NumberFormatter('en_CA', NumberFormatter::PADDING_POSITION);
$formatter->setAttribute(NumberFormatter::SIGNIFICANT_DIGITS_USED, 5);
echo $formatter->format(12345000000);
// Output: 12.345B
$formatter = new NumberFormatter('fr_CA', NumberFormatter::PADDING_POSITION);
$formatter->setAttribute(NumberFormatter::SIGNIFICANT_DIGITS_USED, 5);
echo $formatter->format(12345000000);
// Output: 12,345 G
Or I can do currency formatting like this:
$formatter = new NumberFormatter('en_CA', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(12345000000, 'CAD');
// Output: $12,345,000,000.00
$formatter = new NumberFormatter('fr_CA', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(12345000000, 'CAD');
// Output: 12 345 000 000,00 $
But I can’t combine the two. If I attempt it like so, the $
does not display:
$formatter = new NumberFormatter('en_CA', NumberFormatter::PADDING_POSITION);
$formatter->setAttribute(NumberFormatter::SIGNIFICANT_DIGITS_USED, 5);
echo $formatter->formatCurrency(12345000000, 'CAD');
// Output: 12.345B
$formatter = new NumberFormatter('fr_CA', NumberFormatter::PADDING_POSITION);
$formatter->setAttribute(NumberFormatter::SIGNIFICANT_DIGITS_USED, 5);
echo $formatter->formatCurrency(12345000000, 'CAD');
// Output: 12,345 G
The php.net documentation for NumberFormatter is surprisingly sparse. Is what I’m trying to do impossible or is there some other trick here?
I know I can roll a custom solution here but I would like to be able to rely on built-in l18n methods like I can in JavaScript.