天天看點

MySql格式化小數保留小數點後兩位

方式一:

SELECT FORMAT(12562.6655,2);           

結果:12,562.67

SELECT FORMAT(12332.1,4);           

結果:12,332.1000(小數沒有數字會補0)

檢視文檔:Formats the number X to a format like ‘#,###,###.##’, rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part.整數部分超過三位的時候以逗号分割,并且傳回的結果是string類型的。

方式二

select truncate(4545.1366,2);           

結果:結果:4545.13(直接截取,不會四舍五入)

方式三

select convert(4545.1366,decimal(10,2));           

結果:4545.14

convert()函數會對小數部分進行四舍五入操作,decimal(10,2):它表示最終得到的結果整數部分位數加上小數部分位數小于等于10,小數部分位數2

方式四

round:傳回數字表達式并四舍五入為指定的長度或精度。

文法:ROUND ( numeric_expression , length [ , function ] )

參數:

numeric_expression :精确數字或近似數字資料類型類别的表達式(bit資料類型除外)。

length:是numeric_e-xpression 将要四舍五入的精度。length必須是tinyint、smallint或int。當length為正數時,numeric_e-xpression四舍五入為length所指定的小數位數。當length為負數時,numeric_e-

xpression則按length所指定的在小數點的左邊四舍五入。

function:是要執行的操作類型。function必須是tinyint、smallint或int。如果省略function或function的值為0(預設),numeric_expression将四舍五入。當指定0以外的值時,将截斷numeric_expression。

傳回類型:傳回與numeric_e-xpression相同的類型。

ROUND始終傳回一個值。如果length是負數且大于小數點前的數字個數,ROUND将傳回0。

示例

select ROUND(748.58, -4);           

結果:0

當length是負數時,無論什麼資料類型,ROUND都将傳回一個四舍五入的numeric_e-xpression。

示例

ROUND(748.58, -1);
ROUND(748.58, -2);
ROUND(748.58, -3);           

結果:

750

700

1000

select ROUND(4545.1366,2);            

結果:4545.15

繼續閱讀