将数字格式转换为缩写数字 [英] Format number to abbreviated number

查看:109
本文介绍了将数字格式转换为缩写数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,该数据库的列包含数字格式的值,例如:

I have a database with a column containing values in numeric format , for example:

5,000 for 5k 

86,600 for 86.6k

4,100,000 for 4.1m

在浏览器中显示时,它应该显示5k表示5000,86.6k表示86600等.我需要php中的代码.如果有内置函数可以执行此操作,请提及它.

while displaying in the browser it should display like 5k for 5000 ,86.6k for 86,600,etc..I need the code in php. If there is any built-in function to do this please mention it .

推荐答案

如果将它们用作数字,则可以直接使用此功能.

If you have them as numbers, you can use this function directly.

function formatValue($size, $precision = 1)
{
    static $suffixes = array('', 'k', 'm');
    $base = log($size) / log(1000);

    return round(pow(1000, $base - floor($base)), $precision) . $suffixes[floor($base)];
}

如果您没有数字,请使用 PHP:取消格式化货币

If you don't have them as numbers, use PHP: unformat money

对于您的特殊情况,您可能会使用一些小技巧,但是如果您有更多特殊情况,我不建议您这样做.

For you particular case you may use little dirty hack, but I don't recommend it if you have more special cases.

function formatValue($size, $precision = 1)
{
    static $suffixes = array('', 'k', 'm');
    $base = log($size) / log(1000);

    if ($base >= 5/3 && $base < 2) {
        return round(pow(1000, $base - floor($base)) / 100, $precision) . 'lakh';
    }

    return round(pow(1000, $base - floor($base)), $precision) . $suffixes[floor($base)];
}

这篇关于将数字格式转换为缩写数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆