SQL數值四舍五入小數點后保留2位
1.round() 函數是四舍五入用,第一個參數是我們要被操作的數據,
第二個參數是設置我們四舍五入之后小數點后顯示幾位。
2.numeric 函數的2個參數,第一個表示數據長度,第二個參數表示小數點后位數。
例如: www.2cto.com
select cast(round(12.52,2) as numeric(5,2)) 結果:12.52
select cast(round(12.5525,2) as numeric(5,2)) 結果:12.56
select cast(round(122.52255,2) as numeric(5,2)) 結果:122.52
select cast(round(12222.5255,2) as numeric(5,2)) 結果:報錯了! 原因是:1222.5255,整數位是4,小數位是2,加起來4+2=6,超出了numeric設置的5位,所以為了保險,可以增減numeric的參數,例如numeric(20,2)。
更多信息請查看IT技術專欄