php – 将长号码缩短到K / M / B?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 将长号码缩短到K / M / B?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经GOOGLE了很多,但我找不到任何有用的功能,基于我的查询.

我想要的是:

100 -> 100
1000 -> 1,000
142840 -> 142,840

2023150 -> 2.023M ( i still want 3 addITional numbers for more accuracy )
5430120215 -> 5.430B

我会非常感谢任何自定义功能,以动态选择限制,如果可能的话.

谢谢!

使用number_format():
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,3) . 'M';
} else {
    // At least a billion
    $n_format = number_format($n / 1000000000,3) . 'B';
}

如果“limit”是指小数位数(精度),那很简单

function custom_number_format($n,$PRecision = 3) {
    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,$precision) . 'M';
    } else {
        // At least a billion
        $n_format = number_format($n / 1000000000,$precision) . 'B';
    }

    return $n_format;
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 将长号码缩短到K / M / B?全部内容,希望文章能够帮你解决php – 将长号码缩短到K / M / B?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。