PHP implements RMB digital formatting, adding commas to every three digits
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 08:45:26
Original
1515 people have browsed it
- function num_format($num){
- if(!is_numeric($num)){
- return false;
- }
- $rvalue='';
- $num = explode('.',$num);/ /Separate integers and decimals
- $rl = !isset($num['1']) ? '' : $num['1'];//The value of the decimal part
- $j = strlen($num[0] ) % 3;//How many digits are there in the integer
- $sl = substr($num[0], 0, $j);//Get the number with less than three digits in front of it
- $sr = substr($num[0], $j);//Take out the following three-digit number
- $i = 0;
- while($i <= strlen($sr)){
- $rvalue = $rvalue.','.substr($ sr, $i, 3);//Take out the three digits and combine them, separated by commas
- $i = $i + 3;
- }
- $rvalue = $sl.$rvalue;
- $rvalue = substr($ rvalue,0,strlen($rvalue)-1);//Remove the last comma
- $rvalue = explode(',',$rvalue);//Decompose into an array
- if($rvalue[0]==0) {
- array_shift($rvalue);//If the first element is 0, delete the first element
- }
- $rv = $rvalue[0];//The number with less than three digits in front
- for($i = 1 ; $i < count($rvalue); $i++){
- $rv = $rv.','.$rvalue[$i];
- }
- if(!empty($rl)){
- $rvalue = $rv.'.'.$rl;//The decimal is not empty, integers and decimals are combined
- }else{
- $rvalue = $rv;//The decimal is empty, only integers
- }
- return $rvalue;
- }
Copy code
|
PHP
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31