zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Sql 获取向上取整、向下取整、四舍五入取整的实例详解数据库

实例数据库SQL 详解 获取 取整 四舍五入 向上
2023-06-13 09:20:11 时间

MSSQL取整函数的使用

两个整数相除将截断小数部分 
select 3/4,4/3,5/3 
结果 0,1,1 
 
返回大于或等于所给数字表达式的最小整数 
SELECT CEILING(123.55), CEILING(123.45),CEILING(-123.45), CEILING(0.0) 
结果 124,124,-123,0 
  www.2cto.com  
四舍五入 round(a,b) 结果a 精确到小数点右 b位,或是左 -b位
select round(54.36,-2), round(54.36,-1),round(54.36,0), round(54.36,1),round(54.36,2) 
结果 100.00,50.00,54.00,54.40,54.36 
 
四舍五入 并转化为 整数 
select cast(round(56.361,0) as int),cast(round(56.561,0) as int) 
结果 56,57 
 
举例使用 
 
两个整数相除 舍弃小数部分( 全部都向前进位) 
declare @dividend decimal(20,2), @divisor decimal(20,2) 
 
set @dividend=3 
set @divisor=4 
select CEILING(@[email protected]) 
结果 1 
 
set @dividend=4 
set @divisor=3 
select CEILING(@[email protected]) 
结果 2 
 
set @dividend=5 
set @divisor=3 
select CEILING(@[email protected]) 
结果 2 

两个整数相除 四舍五入到整数 
set @dividend=3 
set @divisor=4 
select cast(round(@[email protected],0) as int) 
结果 1 
 
set @dividend=4 
set @divisor=3 
select cast(round(@[email protected],0) as int) 
结果 1 
 
set @dividend=5 
set @divisor=3 
select cast(round(@[email protected],0) as int) 

结果 2

 

 【四舍五入取整截取】

select round(54.56,0)

【向下取整截取】

SELECT FLOOR(54.56)

 

【向上取整截取】

 SELECT  CEILING(13.15) 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/4726.html