Money vs. Numeric vs. Float: Choosing the Right Datatype for Currency
The use of the Money datatype for currency storage in PostgreSQL has raised questions due to perceived discouragement. This article aims to clarify this issue and provide guidance on the optimal datatype choice.
Contrary to the referenced outdated source, PostgreSQL's official documentation does not indicate any discouragement of the Money datatype. In fact, a recent discussion on pgsql-general, involving core developers, supports its continued use.
For more official information, refer to the Postgres Wiki, which recommends avoiding Money except in specific scenarios. Its primary advantage lies in performance.
For general currency storage, Numeric (alias for Decimal) is widely preferred. The Postgres manual emphasizes its suitability for precise monetary values due to its arbitrary precision.
If fractional Cents are not used, storing currency as an Integer representing Cents is recommended. This approach provides optimal efficiency.
In conclusion, while Money offers performance benefits, Numeric and Integer (for non-fractional Cents) are the recommended datatypes for currency storage in PostgreSQL due to flexibility, precision, and efficiency.
The above is the detailed content of Money, Numeric, or Integer: Which PostgreSQL Datatype is Best for Currency Storage?. For more information, please follow other related articles on the PHP Chinese website!