Home > Database > Mysql Tutorial > mysql 对一个数值进行四舍五入操作

mysql 对一个数值进行四舍五入操作

WBOY
Release: 2016-06-01 09:56:50
Original
1960 people have browsed it

四舍五入函数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>
Copy after login

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>
Copy after login

TRUNCATE(x,y)返回数值x保留到小数点后y位的值(与ROUND最大的区别是不会进行四舍五入)

实例如下:

<code class="language-sql">SELECT TRUNCATE(1.23456,3)
结果:1.234</code>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template