Hi,
I have PHP code that takes care of currency values that I show on a website. The purpose is simple, maintain service/product prices in one script and print PHP variables wherever I need them on the site. here is the code:
function getMoneyUS( $pricesUS ) {
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
return $formatter->formatCurrency($pricesUS, 'USD');
}
function getMoneyUSd( $pricesUS ) {
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 0);
return $formatter->formatCurrency($pricesUS, 'USD');
}
The two functions are for prices with and without decimals (cents in this case).
Then, for each service, I enter the price manually, like in these examples:
$price1 = getMoneyUSd(30);
$price2 = getMoneyUS(74.99);
Now, in some cases, I need to combine prices (bundles), and whatever I tried to add those values produced either an error breaking the page, or $0.00 the best. And even when I get the zero, there would be a "PHP Warning: A non-numeric value encountered"
I tried using some PHP functions to convert string to an integer, and to use number format, but could not make it work. For the $0 result, I would do this:
$bundle = getMoneyUS($price1 + $price2)
Since the numbers are already formatted as currencies, is there a way to add them up (to sum), or I have to do some multiple conversions before getting them out as currencies?
Thank you