select cast(round(12.5,2) as numeric(5,2))
解釋:
round()函數,是四捨五入用,第一個參數是我們要被操作的數據,第二個參數是設置我們四捨五入之後小數點後顯示幾位。
numeric函數的2個參數,第一個表示數據長度,第二個參數表示小數點後位數。
cast as numeric(5,2)函數,是爲了四捨五入之後的數字能確保顯示小數點。
例如:
1.
select cast(round(12.5,2) as numeric(5,2))
結果:12.50
2.
select cast(round(12.555,2) as numeric(5,2))
結果:12.56
3.
select cast(round(122.5255,2) as numeric(5,2))
結果:122.53
4.
select cast(round(1222.5255,2) as numeric(5,2))
結果:報錯了,因爲1222.5255,整數位是4,小數位是2,加起來4+2=6,超出了numeric設置的5位,所以爲了保險期間,可以
增減numeric的參數,例如numeric(20,2)