Currently I have stored values based on percentages of order subtotals out to a precision of 4 decimal places in the database. I need to show a balance as a total sum of those percentages. I have an account with two calculated percentage totals, both equaling 2.8430. So to create a display price I add them up 2.8430 + 2.8430 = 5.6860 and send that value to the toCurrency() function of the \Magento\Framework\Locale\CurrencyInterface class.


* @var $store \Magento\Store\Model\StoreManagerInterface
* @var $currency \Magento\Framework\Locale\CurrencyInterface
$store = $this->storeManager->getStore();
$currency = $this->currency->getCurrency($store->getBaseCurrencyCode());
$value = $currency->toCurrency(sprintf("%f", $value), $options);

However, that's displaying $5.69 when it should be displaying $5.68 in currencies with a precision of 2. I can't find a method in the currency class that will round numbers based on currency precision and I need to do that in order to display the price correctly as well as run some conditional checks on the values to make sure customer requests for withdrawals aren't greater than the sum (5.68).

I've resorted to sending the values to the toCurrency() function, removing the symbol, and running the string result through floatval(), but I feel like this is a hack and I'm wondering if there's a more appropriate way?

$currencyPrecision = $currentAccount->convertToPrice($value, ['display' => \Zend_Currency::NO_SYMBOL]);
$availableAmount = floatval($currencyPrecision);
  • 5.6860 is rounded to 5.69 - this is correct. To achive your desired result, imho you must round the two percentages before you calculate the final percentage. this would be 2.84 + 2.84 = 5.68 Commented Feb 17, 2018 at 20:19
  • Correct, but I need the sum, so how do I round those 2.843 to 2.84 based on the precision of the currency? Currently, I'm running them both through that last block, which gives me a string value, which I then convert back to a float via floatval().
    – Nfourteen
    Commented Feb 17, 2018 at 20:24
  • can you show the code where the values get summed please? Commented Feb 17, 2018 at 20:26
  • 2.843 is stored in the database. So the values are just pulled from the rows of the database. I'm writing the function to sum them now for display, but I don't know how to round them based on the currency to avoid rounding display errors. I'm asking if there's a better way to round them than to send them to the toCurrency() method, implemented in Zend_Currency, with no symbol, get the string value back and then send that string through floatval().
    – Nfourteen
    Commented Feb 17, 2018 at 20:47

2 Answers 2


I found Magento\Directory\Model\Currency, which I think will work.


Please use this code to remove precision from the decimal point.

$value = $currency->toCurrency(sprintf("%f", $value), array('precision' => 0));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.