title: How to handle precision when dealing with currency in MySQL and PHP
body: <p>I have three fields in an invoice table:</p>
  
  <pre><code>subtotal    decimal(12,4)
  tax_amount  decimal(12,4)
  total       decimal(12,4)
  </code></pre>
  
  <p>I have run into a problem with rounding. So there's an invoice with the following values. The addition adds up, but when displaying the invoice data and generating a pdf for the invoice, the displayed data doesn't always add up.</p>
  
  <pre><code>           stored value     displayed (rounded)
                                  value
  subtotal    165.1610         165.16 
  tax_amount  24.7742          24.77
  total       189.9352         189.94
  </code></pre>
  
  <p>The stored values add up. The <code>total</code> column is calculated by adding the <code>subtotal</code> and <code>tax_amount</code> values in PHP. The rounding was done correctly but 165.16 + 24.77 = <strong>189.93</strong> and NOT 189.94.</p>
  
  <p>How can I handle these situations? This is not always the case.</p>