在SQL Server中,如何将-13422.8450四舍五入为-13422.84

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

How do I round -13422.8450 to -13422.84 in SQL Server

问题

我想在SQL Server中将-13422.8450舍入为-13422.84

但是使用ROUND(-13422.8450, 2)返回的是-13422.85

这是完整的查询:

SELECT ROUND(CAST(-1 * 50 * 26845.69 / 100 AS NUMERIC(13, 4)), 2)
英文:

I want to round off -13422.8450 as -13422.84 in SQL Server.

But using ROUND(-13422.8450, 2) is returning -13422.85

This is the full query:

SELECT ROUND(CAST(-1 * 50 * 26845.69 / 100 AS NUMERIC(13, 4)), 2)

答案1

得分: 3

ROUND 函数添加一个非零值的第三个参数;该函数将截断而不是四舍五入:

SELECT ROUND(-13422.8450, 2)
-- -13422.8500

SELECT ROUND(-13422.8450, 2, 1)
-- -13422.8400

请注意,从数字到数字的转换在调用 ROUND 函数之前会四舍五入结果。在您的示例中,您需要先进行四舍五入(截断),然后再进行类型转换。

英文:

Add a third parameter having non-zero value to ROUND function; the function will truncate instead of rounding:

SELECT ROUND(-13422.8450, 2)
-- -13422.8500

SELECT ROUND(-13422.8450, 2, 1)
-- -13422.8400

Be advised that casting from numeric to numeric will round the result before the round function is called. In your example you need to round (truncate), then cast.

huangapple
  • 本文由 发表于 2023年5月24日 23:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325018.html
匿名

发表评论

匿名网友

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

确定