PHP回声数字与字母

PHP回声数字与字母

问题描述:

On my game, I have it to where when someone logs in, it displays their cash. It's Fetched from the database.

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

Well, I updated the database for my account and gave myself $10,000,000,000,000 = 10 Trillion dollars. Well How would I make it to where a user's cash would show like this. If they had $16,500 It would display $16.5K And same with millions billions ETC.
Example: $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
Nothing really showed up on Google. Thanks For Any Help!

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

 &lt; div id =“StatText”&gt; Cash:&lt; span id =“Cash”&gt; $&lt;?php echo number_format(  $ cash)?&gt;&lt; / span&gt;&lt; / div&gt; 
  code>  pre> 
 
 

好吧,我为我的帐户更新了数据库并给了自己$ 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
没有真正出现在谷歌上。 谢谢你的帮助! p> div>

(Late answer) This works (you're welcome to use it) Keep the accepted answer.

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

Call it my contribution

Try this:

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

to get a nice number:

echo bd_nice_number(your_number_here);

Here is the Solution, hope you like and understand that:

<?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, '.', ',')));
    }
?>

Example Output: http://demo.meinchat.net/TEST/