Sql Server 裡的向上取整、向下取整、四捨五入取整的例項!

CalmReason發表於2015-09-11

http://xitong.iteye.com/blog/1865788

==================================================== 【四捨五入取整擷取】
select round(54.56,0)
==================================================== 【向下取整擷取】
SELECT FLOOR(54.56)
==================================================== 【向上取整擷取】
SELECT CEILING(13.15)

以下轉自:http://www.2cto.com/database/201209/156996.html
–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(@dividend/@divisor)
–結果 1

set @dividend=4
set @divisor=3
select CEILING(@dividend/@divisor)
–結果 2

set @dividend=5
set @divisor=3
select CEILING(@dividend/@divisor)
–結果 2

—兩個整數相除 四捨五入到整數
set @dividend=3
set @divisor=4
select cast(round(@dividend/@divisor,0) as int)
–結果 1

set @dividend=4
set @divisor=3
select cast(round(@dividend/@divisor,0) as int)
–結果 1

set @dividend=5
set @divisor=3
select cast(round(@dividend/@divisor,0) as int)
–結果 2

==================================================== 【四捨五入取整擷取】
select round(54.56,0)
==================================================== 【向下取整擷取】
SELECT FLOOR(54.56)
==================================================== 【向上取整擷取】
SELECT CEILING(13.15)

相關文章