phpnew Posted January 18 Share Posted January 18 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 Quote Link to comment https://forums.phpfreaks.com/topic/317634-add-sum-currency-values/ Share on other sites More sharing options...
Solution kicken Posted January 18 Solution Share Posted January 18 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. Quote Link to comment https://forums.phpfreaks.com/topic/317634-add-sum-currency-values/#findComment-1614201 Share on other sites More sharing options...
phpnew Posted January 18 Author Share Posted January 18 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 Quote Link to comment https://forums.phpfreaks.com/topic/317634-add-sum-currency-values/#findComment-1614208 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.