Har*_*jan 1 postgresql aggregate select
在 postgresql 中使用 ceil() 时,我发现它有一些奇怪的行为,请参阅 SQL :
Select
*,
ceil( (550*0.5)/100 ),
ceil( (58*5)/100 ),
FROM
XYZ
Run Code Online (Sandbox Code Playgroud)
如果我专注于 ceil 函数的输出,那就很奇怪了:
如果第二条语句接近更大的整数 3 ,为什么它返回 2 ?
描述
PostgreSQL ceil 函数返回大于或等于数字的最小整数值。
除非另有说明,任何给定形式的函数都返回与其参数相同的数据类型。
正如您所看到的,它工作正常,事实上,如果您使用整数,则返回值是一个整数,(58 * 5) / 100)
返回 2,但如果您将其强制为十进制,(58.0 * 5.0) / 100.0
则返回 2.9。
Run Code Online (Sandbox Code Playgroud)select (550 * 0.5) / 100 as C1, ceil((550 * 0.5) / 100) as CEIL_C1, (58 * 5) / 100 as C2, ceil((58 * 5) / 100) as CEIL_C2, (58.0 * 5.0) / 100.0 as C3, ceil((58.0 * 5.0) / 100.0) as CEIL_C3 ;
c1 | ceil_c1 | c2 | ceil_c2 | c3 | ceil_c3 -----------------: | ------: | -: | :------ | -----------------: | ------: 2.7500000000000000 | 3 | 2 | 2 | 2.9000000000000000 | 3
dbfiddle在这里