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


当前回答

一个更普遍的版本:

function nFormatter(num, digits) { const lookup = [ { value: 1, symbol: "" }, { value: 1e3, symbol: "k" }, { value: 1e6, symbol: "M" }, { value: 1e9, symbol: "G" }, { value: 1e12, symbol: "T" }, { value: 1e15, symbol: "P" }, { value: 1e18, symbol: "E" } ]; const rx = /\.0+$|(\.[0-9]*[1-9])0+$/; var item = lookup.slice().reverse().find(function(item) { return num >= item.value; }); return item ? (num / item.value).toFixed(digits).replace(rx, "$1") + item.symbol : "0"; } /* * Tests */ const tests = [ { num: 0, digits: 1 }, { num: 12, digits: 1 }, { num: 1234, digits: 1 }, { num: 100000000, digits: 1 }, { num: 299792458, digits: 1 }, { num: 759878, digits: 1 }, { num: 759878, digits: 0 }, { num: 123, digits: 1 }, { num: 123.456, digits: 1 }, { num: 123.456, digits: 2 }, { num: 123.456, digits: 4 } ]; tests.forEach(function(test) { console.log("nFormatter(" + test.num + ", " + test.digits + ") = " + nFormatter(test.num, test.digits)); });

其他回答

最多支持数量。MAX_SAFE_INTEGER到Number。MIN_SAFE_INTEGER

function abbreviateThousands(value) { const num = Number(value) const absNum = Math.abs(num) const sign = Math.sign(num) const numLength = Math.round(absNum).toString().length const symbol = ['K', 'M', 'B', 'T', 'Q'] const symbolIndex = Math.floor((numLength - 1) / 3) - 1 const abbrv = symbol[symbolIndex] || symbol[symbol.length - 1] let divisor = 0 if (numLength > 15) divisor = 1e15 else if (numLength > 12) divisor = 1e12 else if (numLength > 9) divisor = 1e9 else if (numLength > 6) divisor = 1e6 else if (numLength > 3) divisor = 1e3 else return num return `${((sign * absNum) / divisor).toFixed(divisor && 1)}${abbrv}` } console.log(abbreviateThousands(234523452345)) // 234.5b (billion) console.log(abbreviateThousands(Number.MIN_SAFE_INTEGER)) // -9.0q (quadrillion)

韦伦·弗林解决方案的2020版。

const SI_SYMBOLS = ["", "k", "M", "G", "T", "P", "E"];

const abbreviateNumber = (number, minDigits, maxDigits) => {
    if (number === 0) return number;

    // determines SI symbol
    const tier = Math.floor(Math.log10(Math.abs(number)) / 3);

    // get suffix and determine scale
    const suffix = SI_SYMBOLS[tier];
    const scale = 10 ** (tier * 3);

    // scale the number
    const scaled = number / scale;

    // format number and add suffix
    return scaled.toLocaleString(undefined, {
        minimumFractionDigits: minDigits,
        maximumFractionDigits: maxDigits,
    }) + suffix;
};

Tests and examples: const abbreviateNumberFactory = (symbols) => ( (number, minDigits, maxDigits) => { if (number === 0) return number; // determines SI symbol const tier = Math.floor(Math.log10(Math.abs(number)) / 3); // get suffix and determine scale const suffix = symbols[tier]; const scale = 10 ** (tier * 3); // scale the number const scaled = number / scale; // format number and add suffix return scaled.toLocaleString(undefined, { minimumFractionDigits: minDigits, maximumFractionDigits: maxDigits, }) + suffix; } ); const SI_SYMBOLS = ["", "k", "M", "G", "T", "P", "E"]; const SHORT_SYMBOLS = ["", "K", "M", "B", "T", "Q"]; const LONG_SYMBOLS = ["", " thousand", " million", " billion", " trillion", " quadrillion"]; const abbreviateNumberSI = abbreviateNumberFactory(SI_SYMBOLS); const abbreviateNumberShort = abbreviateNumberFactory(SHORT_SYMBOLS); const abbreviateNumberLong = abbreviateNumberFactory(LONG_SYMBOLS); const tests = [1e5, -9e7, [1009999.999, 2], [245345235.34513, 1, 1], [-72773144123, 3] ]; const functions = { abbreviateNumberSI, abbreviateNumberShort, abbreviateNumberLong, }; tests.forEach((test) => { const testValue = Array.isArray(test) ? test : [test]; Object.entries(functions).forEach(([key, func]) => { console.log(`${key}(${testValue.join(', ')}) = ${func(...testValue)}`); }); });

这里有一个简单的解决方案,可以避免所有的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代表什么?

一个更普遍的版本:

function nFormatter(num, digits) { const lookup = [ { value: 1, symbol: "" }, { value: 1e3, symbol: "k" }, { value: 1e6, symbol: "M" }, { value: 1e9, symbol: "G" }, { value: 1e12, symbol: "T" }, { value: 1e15, symbol: "P" }, { value: 1e18, symbol: "E" } ]; const rx = /\.0+$|(\.[0-9]*[1-9])0+$/; var item = lookup.slice().reverse().find(function(item) { return num >= item.value; }); return item ? (num / item.value).toFixed(digits).replace(rx, "$1") + item.symbol : "0"; } /* * Tests */ const tests = [ { num: 0, digits: 1 }, { num: 12, digits: 1 }, { num: 1234, digits: 1 }, { num: 100000000, digits: 1 }, { num: 299792458, digits: 1 }, { num: 759878, digits: 1 }, { num: 759878, digits: 0 }, { num: 123, digits: 1 }, { num: 123.456, digits: 1 }, { num: 123.456, digits: 2 }, { num: 123.456, digits: 4 } ]; tests.forEach(function(test) { console.log("nFormatter(" + test.num + ", " + test.digits + ") = " + nFormatter(test.num, test.digits)); });

下面是一个使用for的选项:

函数numberFormat(d) For (var e = 0;D >= 1000;e + +) { D /= 1000; } return d.toFixed(3) + [", ' k', ' M', ' G'][e]; } let s = numberFormat(9012345678); console.log(s == '9.012 G');