php中带小数的价格怎么将整数部分和小数部分分开写呢?

网页标签get后输出一个带小数的价格,比如:6.8。现在我想把整数部分6用16px的大号字体来写,而小数部分8用12px的小号字体来写,请问我改怎么写呢?代码如下:

<span class="tj"><?php echo (get_post_meta($post->ID, "jiage_value", true); ?></span>

QQ截图20200310185407.jpg

阅读 2.5k
1 个回答

封装了个方法:

/**
 * 拆分数字
 *
 * @param $number
 * @return array
 */
function split_number($number)
{
    $tmp = explode('.', $number);

    $count = count($tmp);

    $int = $str = $decimal = '';

    if ($count >= 1) {
        $int = $tmp[0];
    }

    if ($count > 1) {
        $str = '.';
        $decimal = $tmp[1];

    }

    return [$int, $str, $decimal];
}

list($int, $str, $decimal) = split_number(19.22);

echo "int:{$int}" . PHP_EOL;
echo "str:{$str}" . PHP_EOL;
echo "decimal:{$decimal}" . PHP_EOL;
echo "=" * 20;

list($int, $str, $decimal) = split_number(100);

echo "int:{$int}" . PHP_EOL;
echo "str:{$str}" . PHP_EOL;
echo "decimal:{$decimal}" . PHP_EOL;

前端你可以这样写:

<span class="int"><?php echo $int; ?></span>
<?php echo $str; ?>
<span class="decimal"><?php echo $decimal; ?></span>
推荐问题