4

I'm working on a script that handles calculating dollar amounts and I'm wondering what is the correct, or suggested way to go about doing so.

I'm dealing with dollar and cents, not fractions of cents. I've seen posts such as this which give scenarios where fractions of cents are required, such as stocks. I don't think I need that precision, but before I dismiss it I would like some advice.

Say I'm selling an item for $9.99 with 6.5% tax.

$item_cost = 9.99;
$tax_percent = 0.0650; //6.5%
$tax = $item_cost * $tax_percent;
//tax is 0.38935
$total = $item_cost + $tax; 
//total is 6.37935

I can't charge a user $6.37935. I'm just dealing with dollars and cents. Example continued...

$total = 4.401433
$rounded_total = round($total,2);
echo $rounded_total;
//rounded total is 4.4

While I know $4.4 is the same as $4.40, I can't run this through my payment processor. Is it "safe" to round to 2 decimal places first and then apply number format to 2 decimal places?

$total = 4.401433
$rounded_total = round($total,2);
$formatted_total = number_format($rounded_total,2,'.','');
echo $formatted_total;
//rounded and formatted total is 4.40

Is it "acceptable" to determine amounts due by first rounding to 2 decimal places and then using number format to make sure that rounded number is indeed 2 decimal places?

Community
  • 1
  • 1
user3476345
  • 331
  • 2
  • 6
  • 16

1 Answers1

5

DO NOT use number_format to determine the decimal places. Instead Round it first. Round is a math function and closely related to how the business operate. For example if you have a business rule saying anything after 2 decimal is discarded, round it to 2 decimal first. Number format is merely for displaying purposes. For example to add commas, and to add leading 0 when needed. When you actually save a figure make sure to round it according to the business need and then save in DB (as a final figure)

xelber
  • 4,197
  • 3
  • 25
  • 33
  • That's exactly what I did. I was asking if it's acceptable as long as I'm not accepting fractions of cents. I didn't know if there was an easier (not that it's very taxing) or more precise way of calculating 2 decimal places on currencies. – user3476345 Oct 13 '14 at 05:40
  • That's right, but you also had "using number format to make sure that rounded number is indeed 2 decimal places" Which is not good. (if both rounding and number format is 2, that's fine). But as a rule of thumb, don't let number format dictate the decimals. Whats rounded as the final figure (which is saved) is what end user should see. In other words, number_format should not change the value of it even by a fraction. – xelber Oct 13 '14 at 05:43
  • Sorry, just miscommunication. What I meant by "using number format to make sure that rounded number is indeed 2 decimal places" is that after rounding and the number is 3.3, it actually should be 3.30 – user3476345 Oct 13 '14 at 05:46
  • Yep, in which case your approach is fine. Usually is the way in my experience. Decimals can get quite complicated, specially when sub-totals etc are involved. Only rounding final figures help in order to avoid having fractional differences when end user re-calculate. Also adding in your terms and conditions as to how decimals are handled is useful when money is involved. – xelber Oct 13 '14 at 05:52