Whats the difference between float and numeric in Postgresql?
8
votes
1
answer
12764
views
Reading the Postgresql docs [about the numeric data types](https://www.postgresql.org/docs/12/datatype-numeric.html)
leads me to this question:
why do I get these unexpected results with the data types
Float
(SQL standard) and Numeric
in Postgresql?
For example:
CREATE TEMP TABLE testnum (a numeric, b float);
INSERT INTO testnum VALUES (100,100);
INSERT INTO testnum VALUES (9*9*9,9*9*9);
INSERT INTO testnum VALUES (9^9^9,9^9^9);
SELECT (a/3)*3 AS numeric, (b/3) * 3 AS float FROM testnum;
SELECT (a/5)*5 AS numeric, (b/5) * 5 AS float FROM testnum;
Then run
SELECT (a/3)*3 AS numeric, (b/3) * 3 AS float FROM testnum;
numeric | float
99.9999999999999999 | 100
729.0000000000000000 | 729
In this test Float
looks more accurate than Numeric
.
Which one is the appropriate data type for precise numeric values like currency or inventory quantities?
Asked by turtle
(181 rep)
Oct 28, 2020, 08:18 PM
Last activity: Oct 28, 2020, 09:46 PM
Last activity: Oct 28, 2020, 09:46 PM