如何将数字格式设置为只有两位小数?
答
在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
您也可以以同样的方式使用ROUND(),如果你不想让成千上万的分隔符。 – 2010-01-02 18:52:46