在SQL Server 2005中,当我尝试对存储在float变量中的值进行舍入时,我得到的值不正确。在下面的示例中,我希望对ROUND函数的两次调用都应该返回5.6:
DECLARE @foo float;
DECLARE @bar float;
DECLARE @baz float;
SET @foo = 5.55;
SET @bar = ROUND(@foo, 1) --> 5.5
SET @baz = ROUND(5.55, 1) --> 5.6
我做错了什么?
答案 0 :(得分:6)
我不建议将float
数据类型用于确切的decimal
值。
在这种特殊情况下,您可以将@foo
变量转换为decimal
:
SET @bar = ROUND(CAST(@foo as DECIMAL(10,2)), 1) --> 5.6
What Every Computer Scientist Should Know About Floating-Point Arithmetic