Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

consigliere's avatar

Money with more than 2 decimal points

Hi,

I want to use this library https://github.com/moneyphp/money. ( I need conversions too) The examples only use up to two decimal points.

I have money values like: $ 0.168, oil $0.005, $ 0.1683. max of four scale

How should I store it in the database and use the package?

0 likes
4 replies
jimmck's avatar

Store it as an integer 1680 5 1683 and know your precision. Unless the database has a currency type. Rounding errors add up literally.

2 likes
jekinney's avatar

About 10 years ago or so the USA stock market had issues with trading software using float or decimals that made the companies millions per day by rounding down on payouts by fraction of a cent per transaction.

People have been debating this for decades, but what I do is have a default Currency stored as an integer. Any calculations are based off of that, or store as integer with another column for the money type(usd, yen etc.) Similar to time stamp issues. You default your timestamps to one time zone or use UNIX time and calculate from there.

1 like

Please or to participate in this conversation.