【问题标题】:How to format numbers similar to Stack Overflow reputation format如何格式化类似于 Stack Overflow 信誉格式的数字
【发布时间】:2011-03-11 19:24:28
【问题描述】:

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

例如

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

【问题讨论】:

标签: javascript number-formatting


【解决方案1】:
// 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;
}

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

【讨论】:

    【解决方案2】:

    这是CMSversion 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"
    

    【讨论】:

      【解决方案3】:

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

      npm install --save approximate-number
      

      用法:

      var approx = require('approximate-number');
      approx(123456); // "123k" 
      

      【讨论】:

        【解决方案4】:
         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();
           });
        

        【讨论】:

        • 如果您使用车把,只需使用这个..这是 javascript 中的完美代码。
        【解决方案5】:

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

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

        检查输出结果here

        【讨论】:

        • 实际上,toFixed 是我最初实现 >=10k 的方式,但 replace 让我觉得很脏 ;-) 但
        【解决方案6】:

        这是一个 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');
        }
        

        【讨论】:

          【解决方案7】:

          更新: 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'

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

            • 如果数字小于 10000,则应使用分隔符格式化,如果大于,则应使用公斤指示符和小数点后 1 位(如有必要)格式化。你遗漏了一些细节,利亚姆。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-07
            • 1970-01-01
            • 2013-05-12
            • 2013-08-02
            相关资源
            最近更新 更多