我希望实数例如为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/zh/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