场景

财务干系的数据存储常日会用到小数,那么一样平常利用decimal类型来存储,如打算税率、利润率等,我们一样平常通过程序去打算,但是在导表等某些场景下也可以通过MySQL内置函数来实现。

语法

PHPftruncate天天一个常用MySQL函数roundtruncate等 jQuery

round(x, n) 四舍五入,保留n位小数,跟php一样一样的truncate(x, n) 保留n位小数,但是不四舍五入

利用

--如果不写小数位,那便是整数四舍五入select round(1.09821);+----------------+| round(1.09821) |+----------------+| 1 |+----------------+1 row in set (0.00 sec)select round(1.89821);+----------------+| round(1.89821) |+----------------+| 2 |+----------------+1 row in set (0.00 sec)--四舍五入select round(1.09821, 2);+-------------------+| round(1.09821, 2) |+-------------------+| 1.10 |+-------------------+1 row in set (0.00 sec)select round(1.99121, 2);+-------------------+| round(1.99121, 2) |+-------------------+| 1.99 |+-------------------+1 row in set (0.00 sec)--truncate 不会四舍五入select truncate(1.09821, 2);+----------------------+| truncate(1.09821, 2) |+----------------------+| 1.09 |+----------------------+1 row in set (0.00 sec)select truncate(1.09121, 2);+----------------------+| truncate(1.09121, 2) |+----------------------+| 1.09 |+----------------------+1 row in set (0.00 sec)