PHP实现四舍五入的3种方法

PHP中文网
Release: 2023-02-28 21:36:01
Original
3164 people have browsed it

PHP开发中,有时候我们会遇到四舍五入的运算情况,本文分享了用PHP实现四舍五入的3种方法。

php实现四舍五入的三种方法,分别通过number_format函数、round函数和sprintf格式化输出的方法实现四舍五入。

PHP实现四舍五入的3种方法

1.number_format 方法实现四舍五入

$number = 1234.5678; 
$nombre_format_francais = number_format($number, 2, ',', ' '); 
// 1 234,57 
$english_format_number = number_format($number, 2, '.', ''); 
// 1234.57
Copy after login

2.round方法实现四舍五入

$number = 1234.5678; 
echo round($number ,2); 
//1234.57
Copy after login

3.sprintf 格式化输入实现四舍五入

$formatted = sprintf ("%s有¥%01.2f。",$name, $money); 
echo $formatted;   
//张三有¥123.10。
Copy after login

以上就介绍了PHP实现四舍五入的3种方法,包括了四舍五入,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关文章:

php 数据处理之取整,四舍五入

php中四舍五入取整函数详细介绍

php四舍五入截取浮点型字符串方法总结

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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!