How to format numbers similar to Stack Overflow reputation format

前端 未结 8 954
我在风中等你
我在风中等你 2020-11-27 06:41

I want to convert a number into a string representation with a format similar to Stack Overflow reputation display.

e.g.

  • 999 == \'999\'
  • 1000 =
相关标签:
8条回答
  • 2020-11-27 07:09

    Another approach that produces exactly the desired output:

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

    Check the output results here.

    0 讨论(0)
  • 2020-11-27 07:12

    divide by 1000 then if result is greater than 1 round the number and concantenate a "k" on the end.

    If the result is less than 1 just output the actual result!

    0 讨论(0)
  • 2020-11-27 07:13

    I created an npm (and bower) module to do this:

    npm install --save approximate-number
    

    Usage:

    var approx = require('approximate-number');
    approx(123456); // "123k" 
    
    0 讨论(0)
  • 2020-11-27 07:18
     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();
       });
    
    0 讨论(0)
  • 2020-11-27 07:19
    // 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;
    }
    

    This worked for me. I hope, this will help you. Thanks for asking this question.

    0 讨论(0)
  • 2020-11-27 07:22

    Here is CMS's version in PHP (in case someone needed it, like I did):

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

    Output:

    string(3) "999"
    string(5) "1,000"
    string(5) "9,999"
    string(3) "10k"
    string(5) "10.1k"
    
    0 讨论(0)
提交回复
热议问题