7
    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.

Jake
  • 1,469
  • 4
  • 19
  • 40

5 Answers5

10

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.

Indranil
  • 2,451
  • 1
  • 23
  • 31
9

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.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
4

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;
}
dimme
  • 4,393
  • 4
  • 31
  • 51
2

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);
Val
  • 17,336
  • 23
  • 95
  • 144
1

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
Goms
  • 2,424
  • 4
  • 19
  • 36