patternsqlModerate
PostgreSQL and MONEY data type for currency values
Viewed 0 times
postgresqlmoneyvaluestypeforanddatacurrency
Problem
For a project, I created a table with a column
If not, what's the best data type replacement to handle currency values?
price MONEY NOT NULL column. And I thought it would handle decimals properly, unlike a floating number (i.e. IEEE rounding issues), but I end up having values like $9.94 (string) being returned from the database, instead of 9.94 (numeric). Having to manually manipulate the field value by removing the dollar sign is silly; is there a way to have a MONEY column without a currency sign?If not, what's the best data type replacement to handle currency values?
Solution
What about this?
A money value can be cast to numeric without loss of precision. Conversion to other types could potentially lose precision, and must also be done in two stages:
Source:
http://www.postgresql.org/docs/9.1/static/datatype-money.html
A money value can be cast to numeric without loss of precision. Conversion to other types could potentially lose precision, and must also be done in two stages:
SELECT '52093.89'::money::numeric::float8;Source:
http://www.postgresql.org/docs/9.1/static/datatype-money.html
Code Snippets
SELECT '52093.89'::money::numeric::float8;Context
StackExchange Database Administrators Q#89405, answer score: 12
Revisions (0)
No revisions yet.