PHP回声数字与字母

时间:2014-03-09 02:54:54

标签: php numbers

在我的游戏中,我将它带到有人登录的地方,它会显示现金。它是从数据库中获取的。

<div id="StatText">Cash: <span id="Cash">$<?php echo number_format($cash) ?></span></div>

好吧,我为我的帐户更新了数据库,给了自己$ 10,000,000,000,000 = 10万亿美元。那么如何才能将用户的现金显示在这样的位置。如果他们有16,500美元它将显示16.5万美元和相同的数百亿美元ETC.
例: $ 16,500 = 16.5K
$ 160,500 = 160.5K
$ 1,600,500 = 1.6M
$ 10,000,016,500 = 10B
$ 9,110,000,016,500 = 9.1T
谷歌没有真正出现过。 谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

试试这个:

function bd_nice_number($n) {
    // first strip any formatting;
    $n = (0+str_replace(",","",$n));

    // is this a number?
    if(!is_numeric($n)) return false;

    // now filter it;
    if($n>1000000000000) return round(($n/1000000000000),1).' T';
    else if($n>1000000000) return round(($n/1000000000),1).' B';
    else if($n>1000000) return round(($n/1000000),1).' M';
    else if($n>1000) return round(($n/1000),1).' K';

    return number_format($n);
}

获得一个不错的数字:

echo bd_nice_number(your_number_here);

答案 1 :(得分:0)

以下是解决方案,希望您喜欢并理解:

<?php
    /*
        @param $money       the money
        @param $currency    the currency label
        @param $format      Array with parameters like number_format
    */
    function human_readable_money($money, $currency = '', $format = null) {
        $suffix = '';

        // Trillions
        if($money >= 1000000000000) {
            $money  /= 1000000000000;
            $suffix = 'T';

        // Billions
        } else if($money >= 10000000000 && $money <= 1000000000000) {
            $money  /= 10000000000;
            $suffix = 'B';

        // Millions
        } else if($money >= 1000000 && $money <= 10000000000) {
            $money  /= 1000000;
            $suffix = 'M';

        // Thousand
        } else if($money >= 1000 && $money <= 1000000) {
            $money  /= 1000;
            $suffix = 'K';
        }

        if(!empty($format) && count($format) == 3) {
            $money = number_format($money, $format[0], $format[1], $format[2]);
        }

        return sprintf('%s%s%s', $currency, $money, $suffix);
    }

    // Examples
    /*
        $16,500 = 16.5K 
        $160,500 = 160.5K 
        $1,600,500 = 1.6M 
        $10,000,016,500 = 10B 
        $9,110,000,016,500 = 9.1T
    */

    $examples = array(
        16500,
        160500,
        1600500,
        10000016500,
        9110000016500
    );

    foreach($examples AS $cash) {
        printf('<div id="StatText">Cash: <span id="Cash">%s</span></div>', human_readable_money($cash, '$', array(1, '.', ',')));
    }
?>

示例输出:http://demo.meinchat.net/TEST/

答案 2 :(得分:0)

(延迟回答)这有效(欢迎您使用)保留接受的答案。

<?php
$n = 15500000000; // echoes $15.5B
// $n = 9110000016500; // echoes $9.1T

if ($n < 1000000) {
    // Anything less than a million
    $n_format = number_format($n);
} 

else if ($n < 1000000000) {
    // Anything less than a billion
    $n_format = number_format($n / 1000000, 1) . 'M';
} 

else if ($n < 1000000000000) {
    // Anything less than a trillion
    $n_format = number_format($n / 1000000000, 1) . 'B';
} 

else {
    // At least a trillion
    $n_format = number_format($n / 1000000000000, 1) . 'T';

} 

echo "$" .$n_format;

Demo

称之为我的贡献