Show 1k instead of 1,000
Solution 1
Try this:
http://codepad.viper-7.com/jfa3uK
function restyle_text($input){
$input = number_format($input);
$input_count = substr_count($input, ',');
if($input_count != '0'){
if($input_count == '1'){
return substr($input, 0, -4).'k';
} else if($input_count == '2'){
return substr($input, 0, -8).'mil';
} else if($input_count == '3'){
return substr($input, 0, -12).'bil';
} else {
return;
}
} else {
return $input;
}
}
Basically, I think you're using the substr()
wrong.
Solution 2
Here's a generic way to do this that doesn't require you to use number_format
or do string parsing:
function formatWithSuffix($input)
{
$suffixes = array('', 'k', 'm', 'g', 't');
$suffixIndex = 0;
while(abs($input) >= 1000 && $suffixIndex < sizeof($suffixes))
{
$suffixIndex++;
$input /= 1000;
}
return (
$input > 0
// precision of 3 decimal places
? floor($input * 1000) / 1000
: ceil($input * 1000) / 1000
)
. $suffixes[$suffixIndex];
}
And here's a demo showing it working correctly for several cases.
Solution 3
I re-wrote the function to use the properties of numbers rather than playing with strings.
That should be faster.
Let me know if I missed any of your requirements:
function restyle_text($input){
$k = pow(10,3);
$mil = pow(10,6);
$bil = pow(10,9);
if ($input >= $bil)
return (int) ($input / $bil).'bil';
else if ($input >= $mil)
return (int) ($input / $mil).'mil';
else if ($input >= $k)
return (int) ($input / $k).'k';
else
return (int) $input;
}
Solution 4
I do not want to spoil the moment... but I think this is a little more simplified.
Just improving @Indranil answer
e.g.
function comp_numb($input){
$input = number_format($input);
$input_count = substr_count($input, ',');
$arr = array(1=>'K','M','B','T');
if(isset($arr[(int)$input_count]))
return substr($input,0,(-1*$input_count)*4).$arr[(int)$input_count];
else return $input;
}
echo comp_numb(1000);
echo '<br />';
echo comp_numb(1000000);
echo '<br />';
echo comp_numb(1000000000);
echo '<br />';
echo comp_numb(1000000000000);
Solution 5
Or you can too use library How it works is here
Juste install composer require stillat/numeral.php
and
<?php
require_once __DIR__.'/vendor/autoload.php';
$formatter = new Stillat\Numeral\Numeral;
$formatter->setLanguageManager(new Stillat\Numeral\Languages\LanguageManager);
$formatter->format(1532, '0a,0'); //Affiche 1.5K
Comments
-
Jake almost 2 years
function restyle_text($input){ $input = number_format($input); $input_count = substr_count($input, ','); if($input_count != '0'){ if($input_count == '1'){ return substr($input, +4).'k'; } else if($input_count == '2'){ return substr($input, +8).'mil'; } else if($input_count == '3'){ return substr($input, +12).'bil'; } else { return; } } else { return $input; } }
This is the code I have, I thought it was working. apparently not.. can someone help since I can't figure this out.
-
Sagive about 10 yearsThis ignores decimal - example: 1250
-
Henrik Petterson about 6 yearsSo does the other answer. =)
-
user9437856 almost 4 yearsYour link is not working. I know this post is too old.
-
rahul sharma about 3 yearsits showing direct value if we have 1200 its should show 1.2k but this code is showing 1k