Official PHP website in most cases is not of much use for people that ask for help in places like here, on the forum. I was certainly first there, looking into four or five examples from 5 to 9 years ago that were just repeating the official ones. I could not use it for what I came up with at the end.
Anyhow, I was able to figure it out, test and get no errors reported while getting the desired result. And here is the full code, for whoever needs it in the future:
function getMoneyUS( $pricesUS ) {
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
return $formatter->formatCurrency($pricesUS, 'USD') . PHP_EOL;
}
function getMoneyUSd( $pricesUS ) {
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 0);
return $formatter->formatCurrency($pricesUS, 'USD') . PHP_EOL;
}
It's for USD in this example. To change the currency you change the locale in the second line, then the currency code in the following one. The first function is for regular prices with decimals, and the second one is for amounts with no decimals. I need this to show the discount amounts that are always shown as rounded numbers, like Save $20 and so on. MAX_FRACTION_DIGITS sets the number of decimals.
Thanks