阅读有关数字数据类型的 Postgresql 文档
让我想到了这个问题:为什么我会在数据类型Float
(SQL 标准)和Numeric
Postgresql 中得到这些意外结果?
例如:
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;
然后运行
SELECT (a/3)*3 AS numeric, (b/3) * 3 AS float FROM testnum;
numeric | float
99.9999999999999999 | 100
729.0000000000000000 | 729
在这个测试中Float
看起来比Numeric
.
对于货币或库存数量等精确数值,哪一种是合适的数据类型?