为什么SQL Server中的舍入行为不直观?
SELECT CAST( 19.845 AS DECIMAL(18, 2)) AS [DecOK]
,CAST(CAST(19.845 AS FLOAT) AS DECIMAL(18, 2)) AS [DecBad]
,CAST(19.845 AS FLOAT) AS [Float]
,CAST(19.845 AS FLOAT) - 19.845 AS [NoDiff]
,ROUND( 19.845 , 2) AS [RoundOK]
,ROUND(CAST(19.845 AS FLOAT), 2) AS [RoundBad]
实际值:
DecOK DecBad Float NoDiff RoundOK RoundBad
19.85 19.84 19.845 0 19.850 19.84
预期:
DecOK DecBad Float NoDiff RoundOK RoundBad
19.85 19.85 19.845 0 19.850 19.85
无论数据类型如何,我都希望值相同。使用FLOAT时,如何保证准确的结果? NoDiff
值是否隐式地将第二个值转换为FLOAT,这就是结果正好为0的原因?
答案 0 :(得分:0)