英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论