<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mekavo Engineering]]></title><description><![CDATA[Mekavo Engineering]]></description><link>https://mekavo.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Dec 2026 01:25:37 GMT</lastBuildDate><atom:link href="https://mekavo.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Why round($x, 2) is a money bug in many countries: currency decimals in a multi-country app]]></title><description><![CDATA[Most money code is written by someone who thinks in dollars, pounds or euros. Those currencies have two decimal places, so round($amount, 2) looks like the…]]></description><link>https://mekavo.hashnode.dev/why-round-x-2-is-a-money-bug-in-many-countries-currency-decimals-in-a-multi-country-app</link><guid isPermaLink="true">https://mekavo.hashnode.dev/why-round-x-2-is-a-money-bug-in-many-countries-currency-decimals-in-a-multi-country-app</guid><category><![CDATA[PHP]]></category><category><![CDATA[Laravel]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[Mekavo]]></dc:creator><pubDate>Mon, 30 Nov 2026 07:58:36 GMT</pubDate><content:encoded><![CDATA[<p>Most money code is written by someone who thinks in dollars, pounds or euros. Those currencies have two decimal places, so <code>round($amount, 2)</code> looks like the safe, boring thing to do. We run <a href="https://mekavo.com">Mekavo</a>, one Laravel app used by small businesses in 86 countries, and that one line was a bug for a good part of them.</p>
<h2>Not every currency has two decimals</h2>
<p>The number of minor-unit digits (ISO 4217 calls it the "exponent") is a property of the currency:</p>
<ul>
<li><strong>0 decimals:</strong> Japanese yen, Korean won, Chilean peso, Paraguayan guaraní and others. A price of ¥1,500 has no cents.</li>
<li><strong>2 decimals:</strong> pound, dollar, euro, Argentine peso and most others.</li>
<li><strong>3 decimals:</strong> Bahraini dinar, Kuwaiti dinar, Omani rial, Jordanian dinar, Tunisian dinar, Iraqi dinar, Libyan dinar.</li>
</ul>
<p>So <code>round($x, 2)</code> does two different wrong things. In Bahrain it <em>throws away</em> a real digit: a price of 1.255 BHD becomes 1.26, and the owner's price quietly changes. In Japan it invents precision that does not exist, and you end up displaying or summing fractions of a yen.</p>
<h2>The rule: never round what the user typed</h2>
<p>The worst version of this bug is not in totals, it is in input. An owner types a price, and the app "tidies" it on save. Now the number in the database is not the number they typed, and they find out when a customer's invoice is off. Our rule became simple: <strong>what the user types is stored as typed</strong>. Rounding only happens where maths creates new digits (tax, discounts, splitting a bill), and only to the currency's own number of decimals.</p>
<pre><code class="language-plaintext">// One helper, used everywhere money is rounded
function moneyRound(float|string $amount, int $decimals): string
{
    return number_format(round((float) $amount, $decimals), $decimals, '.', '');
}

// The country knows its currency's decimals
moneyRound($total, countryCurrencyDecimals($countryCode));
</code></pre>
<p>(In production you will want decimal strings or integer minor units rather than floats for the maths itself; the point here is <em>where</em> the decimals come from.)</p>
<h2>Make it impossible to regress</h2>
<p>A rule like this is easy to break with one innocent <code>round(…, 2)</code> in a new feature. Two things helped us:</p>
<ul>
<li>A test that greps the codebase for <code>round(</code> with a literal <code>2</code> on money fields and fails the build.</li>
<li>Feature tests that save a price in a 3-decimal currency and a 0-decimal currency and assert the exact value comes back.</li>
</ul>
<h2>Checklist</h2>
<ul>
<li>Store the currency's decimals with the country or currency, not as a constant.</li>
<li>Never round on input. Store what was typed.</li>
<li>Round only computed values, and only to the currency's decimals.</li>
<li>Format for display separately (thousands separators, decimal comma) from rounding.</li>
<li>Test with JPY and BHD, not only GBP and USD.</li>
</ul>
<p>If you build software that sells in more than one country, it is worth a search of your codebase for <code>round(</code> today. We learned it the hard way while taking <a href="https://mekavo.com/gb">Mekavo</a> from the UK to the <a href="https://mekavo.com/us">US</a>, <a href="https://mekavo.com/ar">Argentina</a> and beyond.</p>
]]></content:encoded></item></channel></rss>