Home >Backend Development >PHP Tutorial >3 ways to implement rounding in PHP

3 ways to implement rounding in PHP

小云云
小云云Original
2017-11-13 16:59:434469browse

Rounding is an algorithm function developed by PHP. In PHP development, sometimes we encounter rounding operations. This article shares 3 methods of using PHP to achieve rounding.

There are three ways to implement rounding in php, which are achieved through the number_format function, the round function and the sprintf formatted output method.

1.number_format method implements rounding

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

2.round method implements rounding

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

3.sprintf formatted input implements rounding

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

The above are three A method to implement rounding in PHP. Later, the editor will bring you more related tutorials on PHP rounding.

Related recommendations:

php rounding to intercept floating point string method summary

PHP rounding to retain a few decimal places to format the number

PHP rounding method to precise decimal places and rounding

php rounding function

The above is the detailed content of 3 ways to implement rounding in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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