doudiewen9435 2014-03-09 02:54
浏览 38
已采纳

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!

  • 写回答

3条回答 默认 最新

  • drvkf88226 2014-03-09 03:19
    关注

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

    <kbd>Demo</kbd>

    Call it my contribution

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分