집 >데이터 베이스 >MySQL 튜토리얼 >mysql 对一个数值进行四舍五入操作
四舍五入函数ROUND(x)、ROUND(x,y)和TRUNCATE(x,y)
代码如下:
<code class="language-sql">mysql> SELECT ROUND(2.3), ROUND(2.5), ROUND(2.53,1), ROUND(2.55,1); +------------+------------+---------------+---------------+ | ROUND(2.3) | ROUND(2.5) | ROUND(2.53,1) | ROUND(2.55,1) | +------------+------------+---------------+---------------+ | 2 | 3 | 2.5 | 2.6 | +------------+------------+---------------+---------------+ row in set (0.00 sec)</code>
ROUND(x)返回离x最近的整数,也就是对x进行四舍五入处理
ROUND(x,y)返回x保留到小数点后y位的值,在截取时进行四舍五入处理
再看下面实例:
<code class="language-sql">mysql> SELECT TRUNCATE(2.53,1), TRUNCATE(2.55,1); +------------------+------------------+ | TRUNCATE(2.53,1) | TRUNCATE(2.55,1) | +------------------+------------------+ | 2.5 | 2.5 | +------------------+------------------+ row in set (0.00 sec)</code>
TRUNCATE(x,y)返回数值x保留到小数点后y位的值(与ROUND最大的区别是不会进行四舍五入)
实例如下:
<code class="language-sql">SELECT TRUNCATE(1.23456,3) 结果:1.234</code>