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


当前回答

最多支持数量。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)}`); }); });

最多支持数量。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)

你可以用最简单的方法。

$('#attrib-id').val(Number(response.column/1000000).toLocaleString()); // Million

你可以通过将值除以你想要的数字来使用其他属性,比如如果你想在一个数字前面显示“K”,你应该选择number (response.column/1000),以及其他相应的东西。

这篇文章很旧了,但我不知何故找到了这篇文章。所以添加我的输入数字js是一站式的解决方案现在一天。它提供了大量的方法来帮助格式化数字

http://numeraljs.com/

这个函数可以将巨大的数字(正数和负数)转换为读者友好的格式,而不会失去其精度:

function abbrNum(n) { if (!n || (n && typeof n !== 'number')) { return ''; } const ranges = [ { divider: 1e12 , suffix: 't' }, { divider: 1e9 , suffix: 'b' }, { divider: 1e6 , suffix: 'm' }, { divider: 1e3 , suffix: 'k' } ]; const range = ranges.find(r => Math.abs(n) >= r.divider); if (range) { return (n / range.divider).toString() + range.suffix; } return n.toString(); } /* test cases */ let testAry = [99, 1200, -150000, 9000000]; let resultAry = testAry.map(abbrNum); console.log("result array: " + resultAry);