SQL 怎么搞五舍六入,六舍七入,求大神指點
SQL Server 五舍六入,六舍七入
慕哥6287543
2018-09-05 17:17:31
TA貢獻2041條經驗 獲得超4個贊
可以自定義一個round拓展函數來實現這樣的功能,下面的代碼可供參考,具體可以在此基礎上進行調整:
| 12345678910111213141516171819 | create function f_RoundEx(@Value float, --需要幾舍幾入的值@Digit int, --保留小數位數@point int --確定是幾舍)returns floatas begin declare @Factor float, @result float,@fPoint float set @Factor=power(10,@Digit) set @fPoint = (10-@point-1) * 0.1 if @Value < 0 set @result= floor(@Value*@Factor - @fPoint)/ @Factor else set @result = floor(@Value * @Factor + @fPoint)/ @Factor return @resultendgo |
| 1234567 | --測試實例 select dbo.f_RoundEx(1.244, 2,5) , dbo.f_RoundEx(1.245, 2,5) , dbo.f_RoundEx(1.247, 2,5)select dbo.f_RoundEx(1.245, 2,6) , dbo.f_RoundEx(1.246, 2,6) , dbo.f_RoundEx(1.247, 2,6) |
| 12345 | --測試結果---------------------- ---------------------- ----------------------1.24 1.24 1.25 ---------------------- ---------------------- ----------------------1.24 1.24 1.25 |
舉報