Jump to content

[SOLVED] Shorten up large number displays


2DaysAway

Recommended Posts

Crayon Violent, sprintf() doesn't make that possible. You'll have to write your own function.

 

Here is a small one I wrote for this purpose:

 

<?php
function format($number)
{
$suffix = array('', 'K', 'M', 'B');

for($i = 0, $max = sizeof($suffix)-1; $i < $max; $i++) {
	$pos = strpos($number, '.');
	if ($pos !== false) {
		$length = strlen(substr($number, 0, $pos));
	}
	else {
		$length = strlen($number);
	}

	if ($length < 3) {
		break;
	}

	$number = round($number / 1000, 2);
}

return $number . $suffix[$i];
}

echo format(3750000000); // output: 3.75B
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.