我的表格中有以下内容:
create table example (
col_a int4,
col_b int4,
average_col = numeric
)
col_a = 309
col_b = 16
309 / 16 = 19.3125
当我做以下操作时,我只得到19.不是(19.0,或19.3125,19.3):
TRUNC((col_a/col_b),1) = 19
我做错了什么?
我的表格中有以下内容:
create table example (
col_a int4,
col_b int4,
average_col = numeric
)
col_a = 309
col_b = 16
309 / 16 = 19.3125
当我做以下操作时,我只得到19.不是(19.0,或19.3125,19.3):
TRUNC((col_a/col_b),1) = 19
我做错了什么?
积分、浮点数和数字的存储方式不同,它们的数学也不同.这是计算机的一个基本问题,计算机不像我们那样做数学.因为您正在进行整除,Postgs只进行整除.309/16等于19.
您需要至少一个integer转换为数字(不是float,带有第二个参数的trunc
取数字),然后除.
select trunc(309::numeric/16,1)