14

我想将数字转换为格式类似于 Stack Overflow 信誉显示的字符串表示形式。

例如

  • 999 == '999'
  • 1000 == '1,000'
  • 9999 == '9,999'
  • 10000 == '10k'
  • 10100 == '10.1k'
4

8 回答 8

26

另一种完全产生所需输出的方法:

function getRepString (rep) {
  rep = rep+''; // coerce to string
  if (rep < 1000) {
    return rep; // return the same number
  }
  if (rep < 10000) { // place a comma between
    return rep.charAt(0) + ',' + rep.substring(1);
  }
  // divide and format
  return (rep/1000).toFixed(rep % 1000 != 0)+'k';
}

在此处检查输出结果。

于 2010-07-05T08:19:21.190 回答
10

更新:CMS 得到了检查并提供了一个更好的答案。以他的方式发送更多选票。

// formats a number similar to the way stack exchange sites 
// format reputation. e.g.
// for numbers< 10000 the output is '9,999'
// for numbers > 10000 the output is '10k' with one decimal place when needed
function getRepString(rep)
{
    var repString;

    if (rep < 1000)
    {
        repString = rep;
    }
    else if (rep < 10000)
    {
        // removed my rube goldberg contraption and lifted
        // CMS version of this segment
        repString = rep.charAt(0) + ',' + rep.substring(1);
    }
    else
    {
        repString = (Math.round((rep / 1000) * 10) / 10) + "k"
    }

    return repString.toString();
}

输出:

  • getRepString(999)== '999'
  • getRepString(1000)== '1,000'
  • getRepString(9999)== '9,999'
  • getRepString(10000)== '10k'
  • getRepString(10100)== '10.1k'
于 2010-07-05T07:49:39.337 回答
3

这是 PHP 中的一个函数,它是 iZend 的一部分 - http://www.izend.org/en/manual/library/countformat

function count_format($n, $point='.', $sep=',') {
    if ($n < 0) {
        return 0;
    }

    if ($n < 10000) {
        return number_format($n, 0, $point, $sep);
    }

    $d = $n < 1000000 ? 1000 : 1000000;

    $f = round($n / $d, 1);

    return number_format($f, $f - intval($f) ? 1 : 0, $point, $sep) . ($d == 1000 ? 'k' : 'M');
}
于 2012-06-19T08:02:01.243 回答
1

这是CMS的PHP版本(如果有人需要它,就像我一样):

function getRepString($rep) {
    $rep = intval($rep);
    if ($rep < 1000) {
        return (string)$rep;
    }
    if ($rep < 10000) {
        return number_format($rep);
    }
    return number_format(($rep / 1000), ($rep % 1000 != 0)) . 'k';
}

// TEST
var_dump(getRepString(999));
var_dump(getRepString(1000));
var_dump(getRepString(9999));
var_dump(getRepString(10000));
var_dump(getRepString(10100));

输出:

string(3) "999"
string(5) "1,000"
string(5) "9,999"
string(3) "10k"
string(5) "10.1k"
于 2017-10-21T22:18:24.597 回答
0
 Handlebars.registerHelper("classNameHere",function(rep) {
    var repString;
       if (rep < 1000)
    {
        repString = rep;
    }
    else if (rep < 10000)
    {
        rep = String(rep);
        r = rep.charAt(0);
        s = rep.substring(1);
        repString =  r + ',' + s;
    }
    else
    {
        repDecimal = Math.round(rep / 100) / 10;
        repString = repDecimal + "k";
    }
       return repString.toString();
   });
于 2014-01-07T20:09:20.520 回答
0
// Shortens a number and attaches K, M, B, etc. accordingly
function number_shorten($number, $precision = 3, $divisors = null) {

    // Setup default $divisors if not provided
    if (!isset($divisors)) {
        $divisors = array(
            pow(1000, 0) => '', // 1000^0 == 1
            pow(1000, 1) => 'K', // Thousand
            pow(1000, 2) => 'M', // Million
            pow(1000, 3) => 'B', // Billion
            pow(1000, 4) => 'T', // Trillion
            pow(1000, 5) => 'Qa', // Quadrillion
            pow(1000, 6) => 'Qi', // Quintillion
        );    
    }

    // Loop through each $divisor and find the
    // lowest amount that matches
    foreach ($divisors as $divisor => $shorthand) {
        if (abs($number) < ($divisor * 1000)) {
            // We found a match!
            break;
        }
    }

    // We found our match, or there were no matches.
    // Either way, use the last defined value for $divisor.
    return number_format($number / $divisor, $precision) . $shorthand;
}

这对我有用。我希望这能帮到您。感谢您提出这个问题。

于 2019-12-02T13:55:55.220 回答
-1

除以 1000 则如果结果大于 1,则将数字舍入并在末尾连接一个“k”。

如果结果小于 1,则输出实际结果!

于 2010-07-05T08:59:30.190 回答
-2

我创建了一个npm(和bower)模块来执行此操作:

npm install --save approximate-number

用法:

var approx = require('approximate-number');
approx(123456); // "123k" 
于 2014-11-06T17:26:18.030 回答