我需要显示一个货币值的格式1K等于一千,或1.1K, 1.2K, 1.9K等,如果它不是一个偶数千,否则如果低于一千,显示正常500,100,250等,使用JavaScript格式化的数字?


当前回答

/*including negative values*/    
function nFormatter(num) {
      let neg = false;
       if(num < 0){
         num = num * -1;
         neg = true;
       }
       if (num >= 1000000000) {
         if(neg){
           return -1 * (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'G';  
         }
         return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'G';
       }
       if (num >= 1000000) {
         if(neg){
           return -1 * (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';  
         }
         return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
       }
       if (num >= 1000) {
         if(neg){
           return -1 * (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';  
         }
         return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
       }
       return num;
    }

其他回答

加上上面的答案,这将给出1000的1k而不是1.0k

function kFormatter(num) {
    return num > 999 ? num % 1000 === 0 ? (num/1000).toFixed(0) + 'k' : (num/1000).toFixed(1) + 'k' : num
}

这里有一个简单的解决方案,可以避免所有的if语句(借助Math的力量)。

var SI_SYMBOL = ["", "k", "M", "G", "T", "P", "E"];

function abbreviateNumber(number){

    // what tier? (determines SI symbol)
    var tier = Math.log10(Math.abs(number)) / 3 | 0;

    // if zero, we don't need a suffix
    if(tier == 0) return number;

    // get suffix and determine scale
    var suffix = SI_SYMBOL[tier];
    var scale = Math.pow(10, tier * 3);

    // scale the number
    var scaled = number / scale;

    // format number and add suffix
    return scaled.toFixed(1) + suffix;
}

奖金模因

SI代表什么?

我认为这是一个解决方案。

var unitlist = ["","K","M","G"]; function formatnumber(number){ let sign = Math.sign(number); let unit = 0; while(Math.abs(number) > 1000) { unit = unit + 1; number = Math.floor(Math.abs(number) / 100)/10; } console.log(sign*Math.abs(number) + unitlist[unit]); } formatnumber(999); formatnumber(1234); formatnumber(12345); formatnumber(123456); formatnumber(1234567); formatnumber(12345678); formatnumber(-999); formatnumber(-1234); formatnumber(-12345); formatnumber(-123456); formatnumber(-1234567); formatnumber(-12345678);

听起来这应该对你有用:

函数 kFormatter(num) { 返回 Math.abs(num) > 999 ?Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num) } console.log(kFormatter(1200));1.2k console.log(kFormatter(-1200));-1.2k console.log(kFormatter(900));900 console.log(kFormatter(-900));-900

如果你喜欢,就把功劳归于韦伦·弗林

这比他处理负数和“。0”的情况。

循环和“如果”情况越少,IMO就越好。

function abbreviateNumber(number) {
    const SI_POSTFIXES = ["", "k", "M", "G", "T", "P", "E"];
    const sign = number < 0 ? '-1' : '';
    const absNumber = Math.abs(number);
    const tier = Math.log10(absNumber) / 3 | 0;
    // if zero, we don't need a prefix
    if(tier == 0) return `${absNumber}`;
    // get postfix and determine scale
    const postfix = SI_POSTFIXES[tier];
    const scale = Math.pow(10, tier * 3);
    // scale the number
    const scaled = absNumber / scale;
    const floored = Math.floor(scaled * 10) / 10;
    // format number and add postfix as suffix
    let str = floored.toFixed(1);
    // remove '.0' case
    str = (/\.0$/.test(str)) ? str.substr(0, str.length - 2) : str;
    return `${sign}${str}${postfix}`;
}

jsFiddle测试用例-> https://jsfiddle.net/qhbrz04o/9/