Javascript 有没有办法把数字四舍五入成一种便于阅读的格式?(例如:1.1万美元)

Javascript 有没有办法把数字四舍五入成一种便于阅读的格式?(例如:1.1万美元),javascript,jquery,rounding,currency,Javascript,Jquery,Rounding,Currency,就像Stackoverlow的声誉四舍五入一样,我希望对货币也能做同样的事情 1000美元=>1k 1000000美元=>100万美元 如何在JavaScript中实现这一点(最好是在jQuery中)?下面是一个简单的函数: function abbrNum(number, decPlaces) { // 2 decimal places => 100, 3 => 1000, etc decPlaces = Math.pow(10,decPlaces); /

就像Stackoverlow的声誉四舍五入一样,我希望对货币也能做同样的事情

1000美元=>1k

1000000美元=>100万美元


如何在JavaScript中实现这一点(最好是在jQuery中)?

下面是一个简单的函数:

function abbrNum(number, decPlaces) {
    // 2 decimal places => 100, 3 => 1000, etc
    decPlaces = Math.pow(10,decPlaces);

    // Enumerate number abbreviations
    var abbrev = [ "k", "m", "b", "t" ];

    // Go through the array backwards, so we do the largest first
    for (var i=abbrev.length-1; i>=0; i--) {

        // Convert array index to "1000", "1000000", etc
        var size = Math.pow(10,(i+1)*3);

        // If the number is bigger or equal do the abbreviation
        if(size <= number) {
             // Here, we multiply by decPlaces, round, and then divide by decPlaces.
             // This gives us nice rounding to a particular decimal place.
             number = Math.round(number*decPlaces/size)/decPlaces;

             // Handle special case where we round up to the next abbreviation
             if((number == 1000) && (i < abbrev.length - 1)) {
                 number = 1;
                 i++;
             }

             // Add the letter for the abbreviation
             number += abbrev[i];

             // We are done... stop
             break;
        }
    }

    return number;
}
演示:

演示:


这感觉好极了。我们能得到多小的函数?
函数a(n,d){x=(''+n).length,p=Math.pow,d=p(10,d);x-=x%3;返回Math.round(n*d/p(10,x))/d+“kMGTPE”[x/3]}
——Golfed(108)?:)<代码>abbrNum(999950,0)=>1000k,是的,它只发生50/1M次,但仍然发生。@crizCraig:捕捉得好!我在代码中为此添加了一个特殊的案例处理程序。也许有一种更有效的方法可以做到这一点,但它目前还有效。这目前还不能作为一个可接受的代码问题。请看,您还需要一些测试用例,使其成为社区wiki,最后您应该接受最短(代码大小)的答案。单语言代码高尔夫很无聊,你也应该把它打开给其他语言。视频创建了一个[code golf]版本:谢谢各位——正如你们所看到的,我对Stack Overflow还不熟悉。我很兴奋你们都认为这是一个有趣的问题!这是最好的,因为它也适用于负数
abbrNum(12 , 1)          => 12
abbrNum(0 , 2)           => 0
abbrNum(1234 , 0)        => 1k
abbrNum(34567 , 2)       => 34.57k
abbrNum(918395 , 1)      => 918.4k
abbrNum(2134124 , 2)     => 2.13m
abbrNum(47475782130 , 2) => 47.48b
var pow = Math.pow, floor = Math.floor, abs = Math.abs, log = Math.log;
var abbrev = 'kmb'; // could be an array of strings: [' m', ' Mo', ' Md']

function round(n, precision) {
    var prec = Math.pow(10, precision);
    return Math.round(n*prec)/prec;
}

function format(n) {
    var base = floor(log(abs(n))/log(1000));
    var suffix = abbrev[Math.min(2, base - 1)];
    base = abbrev.indexOf(suffix) + 1;
    return suffix ? round(n/pow(1000,base),2)+suffix : ''+n;
}
> tests = [-1001, -1, 0, 1, 2.5, 999, 1234, 
           1234.5, 1000001, Math.pow(10,9), Math.pow(10,12)]
> tests.forEach(function(x){ console.log(x,format(x)) })

-1001 "-1k"
-1 "-1"
0 "0"
1 "1"
2.5 "2.5"
999 "999"
1234 "1.23k"
1234.5 "1.23k"
1000001 "1m"
1000000000 "1b"
1000000000000 "1000b"