如何绕到最近的便士?

时间:2017-07-10 16:41:08

标签: sql-server tsql rounding

列GrossCommission显示值368.760000但我需要它为368.75。

列NetDueCarrier显示值31.240000但我需要它为31.25

以下是这些值的计算:

SELECT  
        policyNumber, 
        CompanyCommission, 
        CASE WHEN [GrossPremium] < 0 THEN [BondAmount] * (-1) ELSE [BondAmount] END  as [NetPenalLiability],
        GrossPremium as WrittenPremium, 
        PayableFees, 
        (GrossPremium * (CompanyCommissionActual /100)) as GrossCommission, 
        BillDate, 
        PolicyType,
        ((GrossPremium -  (GrossPremium * (CompanyCommissionActual / 100))) + PayableFees) as NetDueCarrier 

enter image description here

2 个答案:

答案 0 :(得分:4)

如果您需要最接近5美分的值,那么您可以使用以下内容:

create table t (val decimal(9,4));
insert into t values
 (31.20),(31.21),(31.22),(31.23),(31.24)
,(31.25),(31.26),(31.27),(31.28),(31.29);

select 
    val
  , Nearest5 = round(val/.05,0)*.05
  -- , Nearest5 = round(val*20,0)/20 -- alternate way of writing the expression
from t

rextester演示:http://rextester.com/UUNA22566

返回:

+---------+----------+
|   val | Nearest5 |
+-------+----------+
| 31.20 | 31.20    |
| 31.21 | 31.20    |
| 31.22 | 31.20    |
| 31.23 | 31.25    |
| 31.24 | 31.25    |
| 31.25 | 31.25    |
| 31.26 | 31.25    |
| 31.27 | 31.25    |
| 31.28 | 31.30    |
| 31.29 | 31.30    |
+-------+----------+

答案 1 :(得分:1)

您可以简单地将计算包装在ROUND函数中:

ROUND(GrossPremium * (CompanyCommissionActual /100), 2) as GrossCommission,
...
ROUND((GrossPremium -  (GrossPremium * (CompanyCommissionActual / 100))) + PayableFees, 2) as NetDueCarrier

更多信息here。这假设您使用版本为&gt; = 2008的SQL Server。