【发布时间】:2021-11-05 01:59:45
【问题描述】:
我正在使用微软SQL Server Express 2016 写一个stored procedure。要求之一是进行四舍五入。但时不时地,四舍五入是错误的。我发现 T-SQL 的四舍五入和 C# 不完全一样,但是为什么呢?
比较下面的两个舍入:
In T-SQL: ROUND(0.045, 2) --> this will produce 0.05
In C#: Math.Round(0.045, 2) --> this will produce 0.04
为什么 C# 生成 0.04?不应该是0.05吗?
我应该怎么做才能使 C# 舍入 = T-SQL 舍入?
出于好奇,我在 C# 中尝试了这个:
Math.Round(0.055, 2)
猜猜 C# 将它四舍五入到什么程度?它四舍五入到 0.06!现在,我完全糊涂了!
Math.Round(0.045, 2) // This becomes 0.04
Math.Round(0.055, 2) // This becomes 0.06
解释是什么?
【问题讨论】:
-
why-does-math-round2-5-return-2-instead-of-3 的可能重复项。乔恩·斯基特本人的回答;)
标签: c# sql-server tsql rounding sql-server-2016-express