So due to the fact that product quantities are not limited to integers but can store decimals up to 4 digits, I can have a product with quantity of 69.4160 square meters of something.
Now assuming the currency is PLN (but I think it doesn't matter) and tax rate is 23%, my cart calculates subtotals and totals for the following products correctly.
Product A: costs 129.00 * 69.4160 = 8954.6640 (11014.2367 with tax) Product B: costs 150.00 * 1 = 150.0000 (184.5000 with tax) These calculations are correct, but for humans I want to display prices up to 2 decimal places hence I call price_float_to_money_format() for cart subtotal, tax and total.
This is my method:
if (!function_exists('price_float_to_money_format')) { function price_float_to_money_format($price): string { // return $price; $formatter = new NumberFormatter('pl_PL', NumberFormatter::CURRENCY); $formatter->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_UP); // up seems wrong // $formatter->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_DOWN); // or down which seems OK? $internationalCurrencySymbol = $formatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL); return $formatter->formatCurrency($price, $internationalCurrencySymbol); } } The following gives me nicely looking prices.
If my subtotal is 9104.6640 it gives me 9 104,67 zł, for tax of 2094.0727 I get 2 094,08 zł but for the whole total of 11198.7367 I get 11 198,74 zł ‼️ when I NumberFormatter::ROUND_UP.
However when I use NumberFormatter::ROUND_DOWN const I get 11 198,73 zł (it also rounds down subtotal from 9 104,67 zł to 9 104,66 zł).
Please help me understand how should I round as in this particular case I think rounding down is correct, because when I manually sum on calc it shows 73 "cents" rather than 74.
Where do I make mistake?
