Home  >  Article  >  Database  >  mysql格式化小数_MySQL

mysql格式化小数_MySQL

WBOY
WBOYOriginal
2016-06-01 13:26:551127browse

bitsCN.com

今天遇到一个问题,格式化浮点数的问题,用format(col,2)保留两位小数点,出现一个问题,例如

 SELECT FORMAT(12562.6655,2);

结果:12,562.67

 查看文档:Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is <code class="literal">0, the result has no decimal point or fractional part.整数部分超过三位的时候以逗号分割,并且返回的结果是string类型的。

 <code class="literal">mysql> SELECT FORMAT(12332.123456, 4);<br>        -> '12,332.1235'<br>mysql> SELECT FORMAT(12332.1,4);<br>        -> '12,332.1000'<br>mysql> SELECT FORMAT(12332.2,0);<br>       -> '12,332'


<code class="literal">没有达到预期结果,想要的结果不要以逗号分隔,

 <code class="literal">select truncate(4545.1366,2);

 <code class="literal">结果:4545.13,直接截取不四舍五入,还是有问题。

 <code class="literal">select convert(4545.1366,decimal);

<code class="literal">结果:4545.14,达到预期。 bitsCN.com
Statement:
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