-- 四捨五入範例
-- 使用「numeric 型態」做小數四捨五入進位處理
--------------------------------------------------------------------------------------------
declare @a1 float, @b1 float, @c1 float, @d1 float, @e1 float
declare @a2 float, @b2 float, @c2 float, @d2 float, @e2 float
/*設定變數初值*/
select @a1 = 0.334, @a2 = 0.335
select @b1 = 0.444, @b2 = 0.445
select @c1 = 0.554, @c2 = 0.455
select @d1 = 0.664, @d2 = 0.465
select @e1 = 0.774, @e2 = 0.475
select @a1 a, @b1 b, @c1 c, @d1 d, @e1 e
select @a2, @b2, @c2, @d2, @e2
print '使用 numeric 型態做小數四捨五入進位處理'
select convert(numeric(5,2),@a1), convert(numeric(5,2),@b1), convert(numeric(5,2),@c1), convert(numeric(5,2),@d1), convert(numeric(5,2),@e1)
select convert(numeric(5,2),@a2), convert(numeric(5,2),@b2), convert(numeric(5,2),@c2), convert(numeric(5,2),@d2), convert(numeric(5,2),@e2)
--------------------------------------------------------------------------------------------
-- 整數相除的小數處理範例
--------------------------------------------------------------------------------------------
declare @a int
declare @b int
declare @c int, @d int, @e int
/*設定變數初值*/
select @a = 1000
select @b = 3
select @c = 43
select @d = 6
select @e = 7
select @a/@b t1, @a/@c t2, @a/@b*1.0 t3, 1.0*@a/@b b, 1.0*@a/@c c, 1.0*@a/@d d, 1.0*@a/@e e
, convert(numeric(5,2),(1.0*@a/@b)) b1
, convert(numeric(5,2),(1.0*@a/@c)) c1
, convert(numeric(5,2),(1.0*@a/@d)) d1
, convert(numeric(5,2),(1.0*@a/@e)) e1
沒有留言:
張貼留言