SQL简单舍入

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

查询:

declare @a float(10);
declare @b float(10);
declare @c float(10);
set @a = 150.50;
set @b = 19;
set @c = 100;
select @a * @b / @c as result
,ROUND(@a * @b / @c, 2) as rounded

结果:

| result  | rounded  |    
----------------------
| 28.595  | 28.59    |

应四舍五入为28.60吗?我该如何实现?

sql-server rounding
1个回答
0
投票

不使用浮点数,而是一些精确的数据类型,例如decimal():

declare @a decimal(19,5);
declare @b decimal(19,5);
declare @c decimal(19,5);
set @a = 150.50;
set @b = 19;
set @c = 100;
select @a * @b / @c as result
,ROUND(@a * @b / @c, 2) as rounded

结果

result  rounded
28.595000   28.600000
© www.soinside.com 2019 - 2024. All rights reserved.