通过将操作数之一转换为非整数来防止整数除法。

huangapple go评论65阅读模式
英文:

Preventing integer division by casting one of the operands as non-integer

问题

需要将分子和分母都强制转换为非整数数据类型以防止整数除法吗?

英文:

Do I have to cast both the numerator and the denominator to a non-integer data type to prevent integer division?

答案1

得分: 0

只需要将一个操作数(要么分子,要么分母)进行类型转换,以防止整数除法,正如手册所述。无需同时进行两者的类型转换:

select 2 / 4;                      -- 0
select 2::numeric / 4;             -- 0.50000000000000000000
select 2 / 4::numeric;             -- 0.50000000000000000000
select 2::numeric / 4::numeric;    -- 0.50000000000000000000

另请参阅:

这些示例中有2次类型转换,而只需要1次类型转换:

英文:

It is enough to cast one of the operands (either the numerator or the denominator) to prevent integer division, as the manual says. Casting both is not necessary:

select 2 / 4;                      -- 0
select 2::numeric / 4;             -- 0.50000000000000000000
select 2 / 4::numeric;             -- 0.50000000000000000000
select 2::numeric / 4::numeric;    -- 0.50000000000000000000

SEE ALSO:

These have 2 casts, while only 1 cast is needed:

huangapple
  • 本文由 发表于 2023年2月7日 02:11:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定