缩短长号到KMB吗?

缩短长号到KMB吗?,第1张

缩短长号到K / M / B吗?

用途

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';}

如果可以的话,我非常感谢任何自定义函数可以动态选择限制。

如果“限制”是指小数位数(精度),则很简单:

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5023844.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存