SELECT
ROUND(58.415, 2), -- rounds UP to 58.420
ROUND(58.4149999999999, 2) -- rounds DOWN to 58.41
这里的问题是当您进行以下变量分配时:
DECLARE @x float
SET @x = 58.415
internally, SQL Server actually stored the value as an approximation, something like 58.41499999999. Then, when rounding to two decimal places, you were left with 58.41.
In general, if you require exact precision, you should use an exact type. In this case, DECIMAL(10,3) would work.
因为第一个参数存储为十进制(5,3):
您有两个不同的代码:
基本上,浮点数是
您所看到的解释是,浮点算术在SQL Server(或任何其他数据库或编程语言)中并不准确。这是实际发生的情况,其中显示了“真实”值以供说明:
这里的问题是当您进行以下变量分配时:
internally, SQL Server actually stored the value as an approximation, something like
58.41499999999
. Then, when rounding to two decimal places, you were left with58.41
.In general, if you require exact precision, you should use an exact type. In this case,
DECIMAL(10,3)
would work.