php add prefix in largest number

no comments
31 Jan 2017 1:53 am

This function can show shorten number from larger numbers or count. #Facebook, #twitter and more other social networks are using this method to show the followers count.

Example a person or celebrity having 95,477,084 followers so through this method it will convert largest number into a small number with sign this 95,477,084 number turn into smallest number 95.47M this smallest number is awesome #UX point can consumed little bit layout area and M signed is used to for million so viewers without counting the numbers can easily say this person have 95.47 Millions followers.

Real Madrid C. F - 21.9 M 21890564 Twitter Followers

Real Madrid C. F – 21.9 M 21890564 Twitter Followers

Here image example to show you how full numbers and small numbers are looking and consuming layout area.

twitter followers count with smallest number and with largest number of followers

twitter followers count with smallest number and with largest number of followers

Here is the function I wrote for all to convert your largest numbers into smallest number.

number_prefix function with 4 parameters $number used for number.

$perception parameter is used for digits are showing after . eg: default is 2 so the result will be 95.47 

$prefixbetween parameter is used for after digit sign eg default is null and – (dash) can be passed so number will 95.75-M

$fullletter parameter default is false if  on false it return M for Million when it true the number will be shown 95.75 Million

function number_prefix($number=0,$perception = 2,$prefixbetwen='',$fullletter=false){
	$number = preg_replace('/[^0-9.]/','',$number);  //numbers only 0 to 9 
		$number = ltrim($number, '0'); //remove first zero eg: 061541
		$number = round($number); 	//remove eg 555.55	
	$number = sprintf('%0.0f',$number);
		$number_len = strlen($number);
			$thousand = 'K';
			$million = 'M';
			$billion = 'B';
			$trillion = 'T';
			$quadrillion = 'Q';
			$septillion = 'S';
			$octillion = 'O';
		} else {
			$thousand = 'Thousands';
			$million = 'Millions';
			$billion = 'Billions';
			$trillion = 'Trillions';
			$quadrillion = 'Quadrillions';
			$septillion = 'Septillions';
			$octillion = 'Octillions';
			case 1:
			case 2:
			return $number;
			case 3:
			case 4:
			case 5:
			case 6:
			return round($number/1000,$perception).$prefixbetwen.$thousand;
			case 7:
			case 8:
			case 9:
			return round($number/1000000,$perception).$prefixbetwen.$million;
			case 10:
			case 11:
			case 12:
			return round($number/1000000000,$perception).$prefixbetwen.$billion;
			case 13:
			case 14:
			case 15:
			case 16:
			case 17:
			return round($number/1000000000000,$perception).$prefixbetwen.$trillion;
			case 18:
			case 19:
			case 20:
			case 21:
			case 22:
			case 23:
			case 24:
			return round($number/1000000000000000,$perception).$prefixbetwen.$quadrillion;
			case 25:
			case 26:
			case 27:
			case 28:
			case 29:
			case 30:
			case 31:
			case 32:
			case 33:
			case 34:
			case 35:
			case 36:
			case 37:
			case 38:
			case 39:
			case 40:
			case 41:
			case 42:
			return round($number/1000000000000000000000000,$perception).$prefixbetwen.$septillion;
			case 43:
			case 44:
			case 45:
			case 46:
			case 47:
			case 48:
			case 49:
			return round($number/10000000000000000000000000000000000000000000,$perception).$prefixbetwen.$octillion;
		return $number;
	return 0;

Here is the examples of function called with parameters

echo number_prefix(“55147″,2,””,false);
echo number_prefix(“651651″,2,””,false);
echo number_prefix(“95,477,084″,2,”-“,true);
echo number_prefix(“95,477,084″,2,””,false);
echo number_prefix(“95,477,084″,2,”-“,false);

called result of number_prefix function

called result of number_prefix function

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>