显示1k而不是1,000

Jak*_*ake 7 php numbers function substr

    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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我认为它有效.显然不是..有人可以提供帮助,因为我无法解决这个问题.

Ind*_*nil 9

试试这个:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,我认为你使用的是substr()错误的.


Mer*_*ham 8

这是执行此操作的通用方法,不需要您使用number_format或执行字符串解析:

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];
}
Run Code Online (Sandbox Code Playgroud)

而且这里是展示它正常工作演示了几种情况.