我需要显示一个货币值的格式1K等于一千,或1.1K, 1.2K, 1.9K等,如果它不是一个偶数千,否则如果低于一千,显示正常500,100,250等,使用JavaScript格式化的数字?
当前回答
最简单最简单的方法是
new Intl.NumberFormat('en-IN', {
notation: "compact",
compactDisplay: "short",
style: 'currency',
currency: 'INR'
}).format(1000).replace("T", "K")
这对任何数字都适用。包括L、Cr等。
注意:不能在safari中工作。
其他回答
Waylon flynn的答案的修改版本,支持负指数:
function metric(number) { const SI_SYMBOL = [ ["", "k", "M", "G", "T", "P", "E"], // + ["", "m", "μ", "n", "p", "f", "a"] // - ]; const tier = Math.floor(Math.log10(Math.abs(number)) / 3) | 0; const n = tier < 0 ? 1 : 0; const t = Math.abs(tier); const scale = Math.pow(10, tier * 3); return { number: number, symbol: SI_SYMBOL[n][t], scale: scale, scaled: number / scale } } function metric_suffix(number, precision) { const m = metric(number); return (typeof precision === 'number' ? m.scaled.toFixed(precision) : m.scaled) + m.symbol; } for (var i = 1e-6, s = 1; i < 1e7; i *= 10, s *= -1) { // toggles sign in each iteration console.log(metric_suffix(s * (i + i / 5), 1)); } console.log(metric(0));
预期的输出:
1.2μ
-12.0μ
120.0μ
-1.2m
12.0m
-120.0m
1.2
-12.0
120.0
-1.2k
12.0k
-120.0k
1.2M
{ number: 0, symbol: '', scale: 1, scaled: 0 }
听起来这应该对你有用:
函数 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
一个简短的替代方案:
function nFormatter(num) { const format = [ { value: 1e18, symbol: 'E' }, { value: 1e15, symbol: 'P' }, { value: 1e12, symbol: 'T' }, { value: 1e9, symbol: 'G' }, { value: 1e6, symbol: 'M' }, { value: 1e3, symbol: 'k' }, { value: 1, symbol: '' }, ]; const formatIndex = format.findIndex((data) => num >= data.value); console.log(formatIndex) return (num / format[formatIndex === -1? 6: formatIndex].value).toFixed(2) + format[formatIndex === -1?6: formatIndex].symbol; }
最多支持数量。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)
简单通用的方法
可以将COUNT_FORMATS配置对象设置为您想要的长度或长度,这取决于您测试的值范围。
// Configuration const COUNT_FORMATS = [ { // 0 - 999 letter: '', limit: 1e3 }, { // 1,000 - 999,999 letter: 'K', limit: 1e6 }, { // 1,000,000 - 999,999,999 letter: 'M', limit: 1e9 }, { // 1,000,000,000 - 999,999,999,999 letter: 'B', limit: 1e12 }, { // 1,000,000,000,000 - 999,999,999,999,999 letter: 'T', limit: 1e15 } ]; // Format Method: function formatCount(value) { const format = COUNT_FORMATS.find(format => (value < format.limit)); value = (1000 * value / format.limit); value = Math.round(value * 10) / 10; // keep one decimal number, only if needed return (value + format.letter); } // Test: const test = [274, 1683, 56512, 523491, 9523489, 5729532709, 9421032489032]; test.forEach(value => console.log(`${ value } >>> ${ formatCount(value) }`));
推荐文章
- 如何在Typescript中解析JSON字符串
- jQuery的“输入”事件
- Javascript reduce()在对象
- 在angularJS中& vs @和=的区别是什么
- 错误"Uncaught SyntaxError:意外的标记与JSON.parse"
- JavaScript中的querySelector和querySelectorAll vs getElementsByClassName和getElementById
- 给一个数字加上st, nd, rd和th(序数)后缀
- 如何以编程方式触发引导模式?
- setTimeout带引号和不带括号的区别
- 在JS的Chrome CPU配置文件中,'self'和'total'之间的差异
- 用javascript检查输入字符串中是否包含数字
- 如何使用JavaScript分割逗号分隔字符串?
- 在Javascript中~~(“双波浪号”)做什么?
- 谷歌chrome扩展::console.log()从后台页面?
- 未捕获的SyntaxError: