dongnao6858 2018-05-02 21:31
浏览 85
已采纳

如何在php中更改小数点的颜色

how can I change the colors of ONLY decimals of a number in PHP? this is my function for formatting numbers

function formatNumber($input, $decimals = 'auto', $prefix = '', $suffix = '') {

    $input = floatval($input);
    $absInput = abs($input);
    if ($decimals === 'auto') {
        if ($absInput >= 0.01) {
            $decimals = 2;
        } elseif (0.0001 <= $absInput && $absInput < 0.01) {
            $decimals = 4;
        } elseif (0.000001 <= $absInput && $absInput < 0.0001) {
            $decimals = 6;
        } elseif ($absInput < 0.000001) {
            $decimals = 8;
        }
    }

 if($input>1000000000000000){
        $result = ROUND(($input/1000000000000000),2).' TH ';
    }elseif($input>1000000000000){
        $result = ROUND(($input/1000000000000),2).'  T ';
    }elseif($input>1000000000){
        $result = ROUND(($input/1000000000),2).'  B ';
    }elseif($input>1000000) {
        $result = ROUND(($input / 1000000), 2) . '  M ';
    } else {
        $result  = number_format($input, $decimals, config('decimal-separator','.'), config('thousand-separator', ',')) ;
    }
    return ($prefix ? $prefix : '') . $result. ($suffix ? $suffix : '');

}

and I use it like that

<?php echo formatNumber($chart['assist'], 2)?>

i want my decimals with a different color... can i use css there or add classes?

  • 写回答

3条回答 默认 最新

  • douke9545 2018-05-02 21:37
    关注

    Here is an example of what I meant in my comment by manipulate the string:

    <?php
    $n = 123.456;
    
    $whole = floor($n);      //  123
    $fraction = $n - $whole; // .456
    
    //echo str_replace('.', '<span class="colorme">.</span>', $n);
    echo $whole . '<span class="colorme">.</span>' . substr($fraction, strpos($fraction, '.')+1);
    

    //Simply do a string replace on the decimal point.

    UPDATED break out parts, concatenate.

    A client side approach with Javascript (with some jQuery) would be something like:

    $('#myDiv').each(function () {
      $(this).html($(this).html().replace(/\./g, '<span class="colorme">.</span>'));
    
      //or decimal point and decimal number part...
      $(this).html($(this).html().replace(/\.([0-9]+)/g, '<span class="colorme">.$1</span>'));
    });
    

    Remember that other locales don't always use . for divider.

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号