// or set by HTTP content negotiation.
$locale ??= Locale::
getDefault();
// Type can be any of the NumberFormatter options, but provide a default.
$type =
(int) ($options['type'
] ?? NumberFormatter::DECIMAL
);
$formatter =
new NumberFormatter($locale,
$type);
// Try to format it per the locale
if ($type === NumberFormatter::CURRENCY
) { $formatter->
setAttribute(NumberFormatter::FRACTION_DIGITS,
$options['fraction'
]);
$output =
$formatter->
formatCurrency($num,
$options['currency'
]);
} else { // In order to specify a precision, we'll have to modify
// the pattern used by NumberFormatter.
$pattern = '#,##0.' .
str_repeat('#',
$precision);
$formatter->
setPattern($pattern);
$output =
$formatter->
format($num);
} // This might lead a trailing period if $precision == 0
$output =
trim($output, '. '
);