MySQL數字的取整 四捨五入 保留n位小數

2021-09-09 07:37:12 字數 1117 閱讀 1446

數學函式是mysql中常用的一類函式。其主要用於處理數字,包括整型和浮點數等等。

mysql常用的四捨五入函式:函式

說明floor(x)

返回不大於x的最大整數。

ceil(x)、ceiling(x)

返回不小於x的最小整數。

truncate(x,d)

返回數值x保留到小數點後d位的值,截斷時不進行四捨五入。

round(x)

返回離x最近的整數,截斷時要進行四捨五入。

round(x,d)

保留x小數點後d位的值,截斷時要進行四捨五入。

format(x,d)

將數字x格式化,將x保留到小數點後d位,截斷時要進行四捨五入。

返回不大於x的最大整數。

select floor(1.3); -- 輸出結果:1

select floor(1.8); -- 輸出結果:1

返回不小於x的最小整數。

select ceil(1.3);    -- 輸出結果:2

select ceiling(1.8); -- 輸出結果:2

返回數值x保留到小數點後d位的值,截斷時不進行四捨五入。

select truncate(1.2328,3); -- 輸出結果:1.232
返回離x最近的整數,截斷時要進行四捨五入。

select round(1.3);  -- 輸出結果:1

select round(1.8); -- 輸出結果:2

保留x小數點後d位的值,截斷時要進行四捨五入。

select round(1.2323,3);  -- 輸出結果:1.232

select round(1.2328,3); -- 輸出結果:1.233

將數字x格式化,將x保留到小數點後d位,截斷時要進行四捨五入。

select format(1.2323,3);  -- 輸出結果:1.232

select format(1.2328,3); -- 輸出結果:1.233

關於數字取整 四捨五入

在做購物車中,涉及購物車小計 產品 為有兩位小數的浮點數,在購物車頁面上從後台獲取資料商品數量及商品 在js中做計算顯示到頁面 出現例如 的情況。解決 採用tofixed 方法進行處理。非同步載入購物車詳情 function url data cart detail page.php success...

四捨五入 上取整 下取整

float tmpfloatdata2 3.7 nsstring tmpstr2 nsstring stringwithformat 0f tmpfloatdata2 nslog tmpstr2 tmpstr2 結果為4 float tmpfloatdata3 6.5 nsstring tmpstr...

C 取整和四捨五入

向上取整 math.ceiling 1 1 math.ceiling 1.1 2 math.ceiling 1.5 2 向下取整 math.float 1 1 math.float 1.1 1 math.float 1.5 1 c 取整函式例項應用詳解 c 取整函式的相關使用是我們在實際開發應用中經...