如何将数字格式设置为只有两位小数?

问题描述:

我希望实数将例如为12.92,但不是12.9241。是否有可能这样做?如何将数字格式设置为只有两位小数?

在PHP中,尝试number_format

$n = 1234.5678; 

// Two decimal places, using '.' for the decimal separator 
// and ',' for the thousands separator. 
$formatted = number_format($n, 2, '.', ','); 
// 1,234.57 

对于PHP,你可以使用number_format(),为MySQL使用FORMAT()功能。

MySQL的:http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_format

FORMAT(number, 2) 

例子:

mysql> SELECT FORMAT(12332.123456, 4); 
     -> '12,332.1235 

PHP:http://php.net/manual/en/function.number-format.php

$number = 1234.5678; 
$formatted_number = number_format($number, 2, '.', ''); 
// 1234.56 
+2

您也可以以同样的方式使用ROUND(),如果你不想让成千上万的分隔符。 – 2010-01-02 18:52:46

你可以乘以100你的电话号码,执行结果,然后鸿沟的舍入返回100.

或者在PHP中使用的圆形功能round function

$result=round(12.9241, 2);