sql round() 函數(shù)四舍五入數(shù)字到指定位數(shù)。它有兩種用法:1. num_digits>0:四舍五入到小數(shù)位;2. num_digits
SQL ROUND() 函數(shù)詳解
SQL ROUND() 函數(shù)用于將數(shù)字四舍五入到指定的位數(shù)。其基本語(yǔ)法為:
ROUND(number, num_di<a style="color:#f60; text-decoration:underline;" href="https://www.php.cn/zt/15841.html" target="_blank">git</a>s)
登錄后復(fù)制
其中:
number:要四舍五入的數(shù)字。
num_digits:指定四舍五入的位數(shù),可以為負(fù)數(shù)或正數(shù)。
使用示例
下表展示了使用 ROUND() 函數(shù)的幾個(gè)示例:
輸入 | num_digits | 輸出 |
---|---|---|
123.4567 | 0 | 123 |
123.4567 | 2 | 123.46 |
123.4567 | -1 | 120 |
123.4567 | 3 | 123.457 |
正負(fù) num_digits 的區(qū)別
num_digits > 0:將數(shù)字四舍五入到指定的小數(shù)位。例如,ROUND(123.4567, 2) 輸出 123.46。
num_digits 將數(shù)字四舍五入到指定整數(shù)位。例如,ROUND(123.4567, -1) 輸出 120。
謹(jǐn)慎使用
在使用 ROUND() 函數(shù)時(shí),請(qǐng)注意以下事項(xiàng):
該函數(shù)只作用于數(shù)字字段,對(duì)非數(shù)字字段無(wú)效。
如果 num_digits 為 0,則將數(shù)字四舍五入到最接近的整數(shù)。
如果 num_digits 為負(fù)數(shù),則將數(shù)字四舍五入到指定的小數(shù)位,直到達(dá)到整數(shù)。
該函數(shù)不會(huì)改變?cè)紨?shù)字,只會(huì)返回四舍五入后的結(jié)果。