Jump to content

Add (sum) currency values

Go to solution Solved by kicken,

Recommended Posts


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

Link to comment
Share on other sites

  • Solution

Formatting values should always be a last-step operation.  You want to structure your code so the prices remain as numbers and you can do whatever operations on them you need to do, then only format them right before you display them, for example directly in your template if possible.

$price1 = 30;
$price2 = 74.99;
$bundle = $price1 + $price2;

//...anything else
//When everything is done and you're ready to display

echo getMoneyUSD($price1);
echo getMoneyUSD($price2);
echo getMoneyUSD($price3);

Also, you should know that floating point math can lead to issues due to lack of precision.  A common way to avoid these issues is to work with your prices as integers (ie, number of cents) then convert them to decimals as part of the formatting process.  Alternatively, you could find a library for dealing with currency values that handles such issues for you.


Link to comment
Share on other sites

Thanks very much.

No wonder I was not able to find a solution. Until now, I was able to get the values out in the money formatted form as there was no need for further use of those variables. I can rework it so it's all numerical until the very end.

Thanks again

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.