Home > Backend Development > PHP Tutorial > PHP_保存两位小数并且四舍五入_保留两位小数并且不四舍五入

PHP_保存两位小数并且四舍五入_保留两位小数并且不四舍五入

WBOY
Release: 2016-06-13 13:03:19
Original
892 people have browsed it

PHP_保留两位小数并且四舍五入_保留两位小数并且不四舍五入
php保留两位小数并且四舍五入

$num = 123213.666666;
echo sprintf("%.2f", $num);
Copy after login



php保留两位小数并且不四舍五入
$num = 123213.666666;
echo sprintf("%.2f",substr(sprintf("%.3f", $num), 0, -2));
Copy after login


php进一法取整
echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
Copy after login


php舍去法,取整数
echo floor(4.3);   // 4
echo floor(9.999); // 9
Copy after login

1 楼 bardo 2011-03-28  
echo round(123213.666666,2);
echo number_format((ceil(123213.666666 * 100))/100,2,'.','');
substr效率很差。这样做是不是更简单?
建议多看看手册吧。

2 楼 kk48323201 2011-03-28  
bardo 写道
echo round(123213.666666,2);
echo number_format((ceil(123213.666666 * 100))/100,2,'.','');
substr效率很差。这样做是不是更简单?
建议多看看手册吧。
谢谢指教..
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template