this post was submitted on 16 Nov 2023
4 points (100.0% liked)
PHP
543 readers
1 users here now
Welcome to /c/php! This is a community for PHP developers and enthusiasts to share and discuss anything related to PHP. From the latest updates and tutorials, to your burning questions and amazing personal projects, we welcome all contributions.
Let's foster an environment of respect, learning, and mutual growth. Whether you're an experienced PHP developer, a beginner, or just interested in learning more about PHP, we're glad to have you here!
Let's code, learn, and grow together!
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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:
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.