php - php改变小数点颜色的方法

标签 php html css

如何在 PHP 中更改数字的小数点颜色? 这是我格式化数字的函数

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 : '');

}

我就是这样使用的

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

我想要不同颜色的小数...我可以在那里使用 css 或添加类吗?

最佳答案

以下是我在评论中通过操作字符串来表达含义的示例:

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

//简单地在小数点上进行字符串替换。

已更新分解各个部分,连接起来。

使用 Javascript(带有一些 jQuery)的客户端方法类似于:

$('#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>'));
});

请记住,其他语言环境并不总是使用 . 作为分隔符。

关于php - php改变小数点颜色的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50143838/

相关文章:

regex - 仅在 HTML5 输入表单中匹配每个 Unicode 字母

css - 打印图例

php - 插入数据库不起作用

php - 如何替换 http ://or www with <a href. 。在 PHP 中

php - 解析html的文本框/输入表单?

php - 从 php 脚本执行 centos 命令

php - 为什么图表的 View 不对称?

html - Markdown 不休息(nobr)?

html - 我如何检查伪元素 webkit-calendar-picker-indicator

jquery - 进度条上的后退按钮