【发布时间】:2021-12-21 15:48:50
【问题描述】:
我正在尝试将值四舍五入到小数点后两位。但是以下代码中的结果仍然显示 4 位小数
select p.ProductName, p.UnitPrice, s.CompanyName, c.CategoryName,
case
when c.CategoryName in ('Condiments', 'Beverages') then round((UnitPrice + (UnitPrice * .2)), 2)
when c.CategoryName in ('meat/poultry', 'seafood') then round((UnitPrice + (UnitPrice * .15)), 2)
when s.CompanyName = 'New Orleans Cajun Delights' then round((UnitPrice + (UnitPrice * .08)), 2)
else p.unitprice
end as NewUnitPrice
from products p
join Categories c on c.CategoryID = p.CategoryID
join Suppliers s on s.SupplierID = p.SupplierID
【问题讨论】:
-
简化(你不应该需要 3 个表格来向我们展示你的舍入问题)-minimal reproducible example.
-
您使用的是哪个 dbms?
-
@jarlh 使用 MSQL
-
else p.unitprice可能是罪魁祸首。将其更改为else ROUND(p.unitprice,2)。 -
@JamesKrueger 您的示例将 NewUnitPrice 显示为六位小数,而不是四位。你能澄清一下问题吗?
标签: sql sql-server tsql rounding