While working on an eCommerce website, sometime we need to convert numbers in appropriate formate. Using normal PHP number formate, we can’t get this conversion so we just added additional into that function.

Below is a function to convert numbers into 1k, 1m, 1b, and 1T. Just copy the below.

function number_format_short( $int_number, $precision = 1 )
{
	if ($int_number < 900)
	{
		// 0 - 900
		$new_number = number_format($int_number, $precision);
		$suffix = '';
	}
	else if ($int_number < 900000)
	{
		// 0.9k-850k
		$new_number = number_format($int_number / 1000, $precision);
		$suffix = 'K';
	} 
	else if ($int_number < 900000000)
	{
		// 0.9m-850m
		$new_number = number_format($int_number / 1000000, $precision);
		$suffix = 'M';
	} 
	else if ($int_number < 900000000000)
	{
		// 0.9b-850b
		$new_number = number_format($int_number / 1000000000, $precision);
		$suffix = 'B';
	}
	else
	{
		// 0.9t+
		$new_number = number_format($int_number / 1000000000000, $precision);
		$suffix = 'T';
	}
	if ( $precision > 0 )
	{
		$dotzero = '.' . str_repeat( '0', $precision );
		$new_number = str_replace( $dotzero, '', $new_number );
	}
	return $new_number . $suffix;
}


Click here to see a demo