Sql 四舍五入至最接近的5美分
我有一项要求,即金额值应四舍五入至小数点后2位Sql 四舍五入至最接近的5美分,sql,sql-server,tsql,rounding,qsqlquery,Sql,Sql Server,Tsql,Rounding,Qsqlquery,我有一项要求,即金额值应四舍五入至小数点后2位 Example: 255.263745 to 255.25 -- 26 taken to 25 57.2115 to 57.20 -- 21 taken to 20 100.75586 to 100.75 我尝试了以下方法,但未能获得所需的输出 select (RIGHT('00000000'+LEFT( CAST(CAST( ISNULL(198.85,0)*ISNULL(128.370,0) AS DECIMAL(
Example:
255.263745 to 255.25 -- 26 taken to 25
57.2115 to 57.20 -- 21 taken to 20
100.75586 to 100.75
我尝试了以下方法,但未能获得所需的输出
select (RIGHT('00000000'+LEFT( CAST(CAST( ISNULL(198.85,0)*ISNULL(128.370,0) AS DECIMAL( 15,0)) AS VARCHAR( 15 )), 8 ), 8 ))
select CEILING(((ISNULL(198.85,0) * (ISNULL(128.370,0)/100)))/.05) *.05
工作样本:
SELECT REPLACE(FORMAT(CAST(ROUND(((ISNULL(198.85,0) * (ISNULL(128.370,0)/100)/05)),2)*05 AS DECIMAL(10,2)),'000000.00'),'.','') AS [Actual Amount]
输出:00025525您可以执行以下操作:
round(mycol / 5, 2) * 5
或者,如果您确实希望严格控制输出数据类型:
cast(round(mycol / 5, 2) * 5 as decimal(10,2))
:
select mycol, cast(round(mycol / 5, 2) * 5 as decimal(10,2)) res
from ( values (255.263745), (57.2115), (100.75586) ) t(mycol)
麦可尔酒店
:--------- | :-----
255.263745 | 255.25
57.211500 | 57.20
100.755860 | 100.75
你可以做:
round(mycol / 5, 2) * 5
或者,如果您确实希望严格控制输出数据类型:
cast(round(mycol / 5, 2) * 5 as decimal(10,2))
:
select mycol, cast(round(mycol / 5, 2) * 5 as decimal(10,2)) res
from ( values (255.263745), (57.2115), (100.75586) ) t(mycol)
麦可尔酒店
:--------- | :-----
255.263745 | 255.25
57.211500 | 57.20
100.755860 | 100.75