四舍五入到最接近的 5 美分

Rounding to the closest 5 cents

我有一个要求,要求金额值应四舍五入到最接近的 5 美分到小数点后两位。

 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))

Demo on DB Fiddle:

select mycol, cast(round(mycol / 5, 2) * 5 as decimal(10,2)) res
from ( values (255.263745), (57.2115), (100.75586) ) t(mycol)
mycol      | res   
:--------- | :-----
255.263745 | 255.25
57.211500  | 57.20 
100.755860 | 100.75