【发布时间】:2010-01-02 18:43:15
【问题描述】:
我希望实数例如是 12.92,而不是 12.9241。这样可以吗?
【问题讨论】:
我希望实数例如是 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
【讨论】:
$number = 1234.5678;
$teX = explode('.', $number);
if(isset($teX[1])){
$de = substr($teX[1], 0, 2);
$final = $teX[0].'.'.$de;
$final = (float) $final;
}else{
$final = $number;
}
最终将是 1234.56
【讨论】:
【讨论】: