2011-12-18 3 views
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; 
    } 
} 

Questo è il codice che ho, ho pensato che funzionasse. apparentemente no ... qualcuno può aiutare perché non riesco a capirlo.Mostra 1k invece di 1.000

+1

Cosa hai ricevuto quando hai eseguito questo codice? Hai ricevuto errori? Se sì allora quale? – Lion

+1

Che cosa sta facendo "non funziona"? –

+0

possibile duplicato di [Accorciare i numeri lunghi su K/M/B?] (Http://stackoverflow.com/questions/4371059/shorten-long-numbers-to-kmb) –

risposta

8

Prova questo:

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; 
    } 
} 

Fondamentalmente, penso che si sta utilizzando il substr() sbagliata.

3

Ho riscritto la funzione per utilizzare le proprietà dei numeri anziché giocare con le stringhe.

Questo dovrebbe essere più veloce.

Fatemi sapere se ho perso le tue esigenze:

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; 
} 
6

Ecco un modo generico per fare questo che non richiede di utilizzare number_format o fare stringa 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]; 
} 

E here's a demo showing it working correctly per diversi casi.

+0

Grazie molto amico, funziona come un fascino –

1

Non voglio rovinare il momento ... ma penso che questo sia un po 'più semplificato.

Proprio migliorare @Indranil risposta

esempio

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); 
+0

Questo ignora decimale - esempio: 1250 –

+0

Così fa l'altra risposta. =) –