Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing round() to get exact value - PHP
    text
    copied!<p>Hey guy's I am working on a project of mine which involves the use of money. I am trying to not use round anymore because, it's rounding things to nearest tenth and I need exact numbers. One reason I was using it, was because it was giving a whole number.</p> <p>The way I am working my number system is that <code>100 = $1, 2000 = $20, etc..</code> I was currently using the round function because it would get rid of the decimal point for me and give me a whole number, lets say: <code>223</code> which in turn would = <code>$2.23</code>.</p> <p>Here is what I am using: </p> <pre><code>$amount += round(($amount / 29) + 30); </code></pre> <p>Here are the numbers:</p> <p>Lets say we have a charge of <code>100 and we add 125 which equal 225 (USD $2.25)</code>. Now we add taxes and processing: <code>+ 2.9% + $.30</code>. After multiplying <code>2.25 by 2.9%</code> and adding <code>.30</code> the amount would be: <code>0.36525</code> - this is the amount that should be added than to the <code>$2.25 which than would be 261 = $2.61</code></p> <p>The issue is because of the rounding, when I look in my Stripe panel <code>(I am using Stripe API for payments)</code> I see a charge of <code>$2.63</code>. So my question is, how would I go about making it exact without having any rounding and decimal places.</p> <p>UPDATE:</p> <p>Here is the above example more explained: </p> <p>Lets say we have a charge of <code>100 and we add 125 which equal 225 (USD $2.25)</code>. Now we add taxes and processing: <code>+ 2.9% + $.30</code>. After multiplying <code>2.25 by 2.9%</code> and adding <code>.30</code> the amount would be: <code>0.36525</code> - this is the amount that should be added than to the <code>$2.25 which than would be 261 = $2.61</code></p> <p>So now with that the actual value of amount that should be charged is <code>$2.61</code> but instead when using the round it gives me <code>263</code> which also means <code>$2.63</code>. The above example is the simple math that is correct.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload