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

3 ways to implement rounding in PHP

PHP中文网
PHP中文网Original
2017-03-16 10:28:303174browse

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.

3 ways to implement rounding in PHP

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 introduces 3 methods of PHP implementing rounding, including rounding, PHP content, I hope it will be helpful to friends who are interested in PHP tutorials.

Related articles:

php data processing rounding and rounding

detailed introduction to the rounding and rounding function in php

php rounding and interception floating point string method summary

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