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

问题描述 投票:1回答:1

我有一项要求,金额金额应四舍五入至最接近的5美分至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( 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

sql sql-server tsql rounding qsqlquery
1个回答
2
投票

您可以做:

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 |资源:--------- | :-----255.263745 | 255.2557.211500 | 57.20100.755860 | 100.75
© www.soinside.com 2019 - 2024. All rights reserved.