I recommend going further than this example and don’t just store the value/currency for your price. You should also store the VAT. And generally anything else that involves division or percentages.
Jump through whatever hoops are necessary to work exclusively with addition and subtraction (and the occasional cheeky multiplication to shortcut repeated addition operations). Percentages and division is where people tend to get into trouble.
Specifically you should avoid the last line of code in this example from the article OP posted:
I’m not sure what they were trying to get at with that example, so here’s a more realistic example where of avoiding percentages:
classProduct{
/**
* The price of the product excluding tax. Entered by the user into the database.
*/public Money $price;
/**
* The amount of VAT to collect for this product. Not entered by the user, but calculated
* whenever the product price changes (or, whenever the tax legislation changes). The
* value is stored in the database as an integer (and currency).
*/public Money $vat;
}
classCartItem{
public Product $product;
publicint$qty;
}
$invoiceTotal = newMoney(0);
$invoiceVat = newMoney(0);
$paymentAmount = newMoney(0);
foreach ($cartItem in $cart) {
$invoiceTotal
->add($cartItem->product->price)
->multiply($cartItem->qty);
$invoiceVat
->add($cartItem->product->vat)
->multiply($cartItem->qty);
$paymentAmount
->add($cartItem->product->price)
->add($cartItem->product->vat)
->multiply($cartItem->qty);
}
Avoiding division should also be done everywhere else - for example if your credit card facility charges 30c+2.9%… don’t fall for the trap of calculating 2.9% of $paymentAmount because chances are you’ll round something in the wrong direction. Also, some cards have higher fees. Instead, when you authorise the payment the credit card facility should tell you that the card fee for that actual transaction is 152 cents. Record that number in your payment record and use it for your own internal reporting on profits/etc.
I recommend going further than this example and don’t just store the value/currency for your price. You should also store the VAT. And generally anything else that involves division or percentages.
Jump through whatever hoops are necessary to work exclusively with addition and subtraction (and the occasional cheeky multiplication to shortcut repeated addition operations). Percentages and division is where people tend to get into trouble.
Specifically you should avoid the last line of code in this example from the article OP posted:
echo $money->plus('4.99'); // USD 54.99 echo $money->minus(1); // USD 49.00 echo $money->multipliedBy('1.999'); // USD 99.95 echo $money->dividedBy(4); // USD 12.50
I’m not sure what they were trying to get at with that example, so here’s a more realistic example where of avoiding percentages:
class Product { /** * The price of the product excluding tax. Entered by the user into the database. */ public Money $price; /** * The amount of VAT to collect for this product. Not entered by the user, but calculated * whenever the product price changes (or, whenever the tax legislation changes). The * value is stored in the database as an integer (and currency). */ public Money $vat; } class CartItem { public Product $product; public int $qty; } $invoiceTotal = new Money(0); $invoiceVat = new Money(0); $paymentAmount = new Money(0); foreach ($cartItem in $cart) { $invoiceTotal ->add($cartItem->product->price) ->multiply($cartItem->qty); $invoiceVat ->add($cartItem->product->vat) ->multiply($cartItem->qty); $paymentAmount ->add($cartItem->product->price) ->add($cartItem->product->vat) ->multiply($cartItem->qty); }
Avoiding division should also be done everywhere else - for example if your credit card facility charges
30c + 2.9%
… don’t fall for the trap of calculating2.9%
of$paymentAmount
because chances are you’ll round something in the wrong direction. Also, some cards have higher fees. Instead, when you authorise the payment the credit card facility should tell you that the card fee for that actual transaction is152
cents. Record that number in your payment record and use it for your own internal reporting on profits/etc.