Home >Backend Development >PHP Tutorial >Three methods of rounding in php

Three methods of rounding in php

WBOY
WBOYOriginal
2016-07-25 08:44:231148browse

There are three methods of rounding in php, which are implemented through the number_format function, the round function and the sprintf formatted output method.

1. The number_format method implements rounding
  1. $number = 1234.5678;
  2. $nombre_format_francais = number_format($number, 2, ',', ' ');
  3. // 1 234,57
  4. $english_format_number = number_format($number, 2, '.', ' ');
  5. // 1234.57
Copy code
2 .round method implements rounding
  1. $number = 1234.5678;
  2. echo round($number ,2);
  3. //1234.57
Copy code
3.sprintf formatted input implements rounding
  1. $formatted = sprintf ("%s has ¥%01.2f.",$name, $money);
  2. echo $formatted;
  3. //Zhang San has ¥123.10.
Copy code
Three types, rounded, php


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