Number formatting, four ways to add commas every three bits from right to left

Source: Internet
Author: User

Reprinted from: http://www.uedsc.com/micrometer-method.html

Sometimes I need to add a number to every three digits plus a comma, for example, 20000 to 20,000, in order to facilitate the financial reading, so I sorted out the following four ways to solve:

1. javascript

 //  a roundabout function    FormatNumber (str) { if  ( Str.length <= 3 return   Str;  else   { return  FormatNumber (Str.substr (0,str.length-3)) + ', ' +str.substr (str.length-3    ); }}  //  test function (note that numbers are to be entered in a string)  var  money = FormatNumber (' 1234567890 '  //  
View Code

2. PHP built-in functions

// Test function  = 1234567890; Echo Number_format (, 3, ', ', '); // The results are: 1,234,567,890
View Code

3. PHP Custom Functions

functionNum_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 a decimal part$j=strlen($num[0])% 3;//how many bits of an integer$SL=substr($num[0], 0,$j);//the number in front of the three digits is taken out$SR=substr($num[0],$j);//the number of full three digits in the back is taken out$i= 0; while($i<=strlen($SR)){$rvalue=$rvalue.‘,‘.substr($SR,$i, 3);//three-bit three-bit remove and merge, separated by commas$i=$i+ 3;}$rvalue=$SL.$rvalue;$rvalue=substr($rvalue, 0,strlen($rvalue)-1);//remove the last comma$rvalue=Explode(‘,‘,$rvalue);//decomposition of an arrayif($rvalue[0]==0){Array_shift($rvalue);//if the first element is 0, the first element is deleted}$RV=$rvalue[0];//number of previous three digits for($i= 1;$i<Count($rvalue);$i++){$RV=$RV.‘,‘.$rvalue[$i];}if(!Empty($RL)){$rvalue=$RV.‘.‘.$RL;//decimal is not empty, integer and decimal are merged}Else{$rvalue=$RV;//decimal is empty, only integer}return $rvalue;}EchoNum_format (' 1234576 ');?>Output 1, 234,576
View Code

4, in the SQL inquiry, the number of the first conversion

--the list salary (salary) has a linked fields bit called MoneySELECTFORMAT ( Money,4) fromsalary;--The result will be the money 's digital form into the XX,XXX,XXX format, the following outputSELECTFORMAT (12332.123456,4);--The results are: 12,332.1235SELECTFORMAT (12332.2,0)--The results are: 12,332SELECTFORMAT (12332.1,4)--The results are: 12,332.1000
View Code

Number formatting, four ways to add commas every three bits from right to left

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.