php number formatting sample code

怪我咯
Release: 2023-03-14 07:36:02
Original
1875 people have browsed it

Many times we need to format numbers, such as adding 0 in front of insufficient digits. It can be easily implemented with PHP, because PHP comes with functions for related functions. The

number_format() function formats a number by thousands grouping.

For example, echo number_format(285266237);

can output 285,266,237

In addition, if you need to format the file byte size , the following method can be used for reference:

 function byte_format($input, $dec=0)   
 {    
  $prefix_arr = array(' B', 'K', 'M', 'G', 'T');   
  $value = round($input, $dec);   
  $i=0;   
  while ($value>1024)   
  {    
   $value /= 1024;   
   $i++;   
  }   
  $return_str = round($value, $dec).$prefix_arr[$i];   
  return $return_str;   
 }   

 echo byte_format(285266237);
Copy after login

The display result is 272M

For example, echo number_format(285266237);
Can output 285,266,237
In addition, if you need to format the file Byte size, the following method can be used for reference:

 function byte_format($input, $dec=0)   
 {    
  $prefix_arr = array(' B', 'K', 'M', 'G', 'T');   
  $value = round($input, $dec);   
  $i=0;   
  while ($value>1024)   
  {    
   $value /= 1024;   
   $i++;   
  }   
  $return_str = round($value, $dec).$prefix_arr[$i];   
  return $return_str;   
 }   

 echo byte_format
Copy after login

The above is the detailed content of php number formatting sample code. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!