Jump to content

[SOLVED] numformat


2DaysAway

Recommended Posts

I need this function to figure up to quadrillion. I know that isn't even right for trillion. I'm sure it's something silly and small but I'm having a brainfart, any help please?

 

function hformat($number)
{
$suffix = array('', ' K', ' M', ' B',' T');

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

	if ($length < 4) {
		break;
	}

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

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

echo hformat($hint); 

Link to comment
https://forums.phpfreaks.com/topic/126656-solved-numformat/
Share on other sites

try

<?php
function hformat($number) {
$suffix = array('', ' K', ' M', ' B',' T', ' Q');
$max = count($suffix) - 1;
while ($number >= 1000 and $i < $max){
	$number /= 1000;
	$i++;
}
$number = round($number, 2);
return $number . $suffix[$i];
}
echo hformat(1234567891234567);
?>

Link to comment
https://forums.phpfreaks.com/topic/126656-solved-numformat/#findComment-655001
Share on other sites

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.