簡體   English   中英

如何在 JavaScript 中四舍五入?

[英]How do I round millions and thousands in JavaScript?

我正在嘗試舍入大數字。 例如,如果我有這個號碼:

12,645,982

我想四舍五入這個數字並將其顯示為:

13 mil

或者,如果我有這個號碼:

1,345

我想將其四舍五入並將其顯示為:

1 thousand

我如何在 JavaScript 或 jQuery 中執行此操作?

這是一個用於格式化數千、數百萬和數十億的實用函數:

function MoneyFormat(labelValue) 
  {
  // Nine Zeroes for Billions
  return Math.abs(Number(labelValue)) >= 1.0e+9

       ? Math.abs(Number(labelValue)) / 1.0e+9 + "B"
       // Six Zeroes for Millions 
       : Math.abs(Number(labelValue)) >= 1.0e+6

       ? Math.abs(Number(labelValue)) / 1.0e+6 + "M"
       // Three Zeroes for Thousands
       : Math.abs(Number(labelValue)) >= 1.0e+3

       ? Math.abs(Number(labelValue)) / 1.0e+3 + "K"

       : Math.abs(Number(labelValue));

   }

用法:

   var foo = MoneyFormat(1355);
   //Reformat result to one decimal place
   console.log(parseFloat(foo).toPrecision(2) + foo.replace(/[^B|M|K]/g,""))

參考

數字 JS .. 如果有人檢查這個,請檢查數字 Js。 你只需要包含腳本,然后它只是一行代碼

numeral(yourNumber).format('0.0a')
var lazyround = function (num) {
    var parts = num.split(",");
    return parts.length > 1 ? (Math.round(parseInt(parts.join(""), 10) / Math.pow(1000, parts.length-1)) + " " + ["thousand", "million", "billion"][parts.length-2]) : parts[0];
};

alert(lazyround("9,012,345,678"));
alert(lazyround("12,345,678"));
alert(lazyround("345,678"));
alert(lazyround("678"));

它輸出這個:

9 billion
12 million
346 thousand
678

玩得開心。 這工作正常,因為我沒有看到你自己做了任何事情,所以這被混淆了。

在 jsfiddle 中有一個工作示例: jsfiddle.net/p8pfB/

@Paul 的回答很好,但它給出了 773.282600918 M 等。

我們可能不希望它用於大數字。 使用 Math.round() 修復

function moolah_round(num,locale='en') {
    // Nine Zeroes for Billions
    return Math.abs(Number(num)) >= 1.0e+9
        ? Math.round(Math.abs(Number(num)) / 1.0e+9 ) + " B"
        // Six Zeroes for Millions
        : Math.abs(Number(num)) >= 1.0e+6
            ? Math.round(Math.abs(Number(num)) / 1.0e+6 ) + " M"
            // Three Zeroes for Thousands
            : Math.abs(Number(num)) >= 1.0e+3
                ? Math.round(Math.abs(Number(num)) / 1.0e+3 ) + " K"
                : Math.abs(Number(num)); 
}
var number = 1345;
var zeroCount = 3;
var roundedNumber = Math.round( number / Math.pow(10,zeroCount) )

只需將 zeroCount 調整為 3 為千,6 為百萬,等等。我假設您可以使用 if 語句並且只需要一些數學函數的幫助。

保羅·斯威特的回答很好

迪帕克托馬斯的回答很好

我的答案是更靈活/可定制且更易於閱讀,所以這里是:

代碼

 function NumbFormat(options) { let number = Math.abs(Number(options.number)); // Nine zeros for Billions if (Number(number) >= 1.0e+9) { return (number / 1.0e+9).toFixed(options.billion.decimal) + ` ${options.billion.unit}`; } // Six zeros for Millions if (Number(number) >= 1.0e+6) { return (number / 1.0e+6).toFixed(options.million.decimal) + ` ${options.million.unit}`; } // Thrhee zeros for Thousands if (Number(number) >= 1.0e+3) { return (number / 1.0e+3).toFixed(options.thousand.decimal) + ` ${options.thousand.unit}`; } return number; } console.log(NumbFormat({ 'number': 100000000, 'billion': { 'decimal': 1, 'unit': 'B', }, 'million': { 'decimal': 1, 'unit': 'M', }, 'thousand': { 'decimal': 1, 'unit': 'K', }, })); console.log(NumbFormat({ 'number': 100000000, 'billion': { 'decimal': 1, 'unit': 'B', }, 'million': { 'decimal': 1, 'unit': 'Million', }, 'thousand': { 'decimal': 1, 'unit': 'K', }, })); console.log(NumbFormat({ 'number': 100000000, 'billion': { 'decimal': 1, 'unit': 'B', }, 'million': { 'decimal': 0, 'unit': 'Million', }, 'thousand': { 'decimal': 1, 'unit': 'K', }, })); console.log(NumbFormat({ 'number': 100000000, 'billion': { 'decimal': 1, 'unit': 'B', }, 'million': { 'decimal': 0, 'unit': 'M', }, 'thousand': { 'decimal': 1, 'unit': 'K', }, }));

使用上面的代碼,您可以控制十億、百萬或千的小數位數,您還可以控制要顯示的單位:)

我修改了 cetinajero 和 Deepak Thomas 的精彩答案以處理負數。 (並且不使用三元組)

 const numRound = (number) => { let num = number; num = Number(num); const billions = num / 1.0e9; const millions = num / 1.0e6; const thousands = num / 1.0e3; if (Math.abs(num) >= 1.0e9 && Math.abs(billions) >= 100) { return `${Math.round(billions)}B` } if (Math.abs(num) >= 1.0e9 && Math.abs(billions) >= 10) { return `${billions.toFixed(1)}B` } if (Math.abs(num) >= 1.0e9) { return `${billions.toFixed(2)}B` } if (Math.abs(num) >= 1.0e6 && Math.abs(millions) >= 100) { return `${Math.round(millions)}M` } if (Math.abs(num) >= 1.0e6 && Math.abs(millions) >= 10) { return `${millions.toFixed(1)}M` } if (Math.abs(num) >= 1.0e6) { return `${millions.toFixed(2)}M` } if (Math.abs(num) >= 1.0e3 && Math.abs(thousands) >= 100) { return `${Math.round(thousands)}K` } if (num >= 1.0e3 && thousands >= 10) { return `${thousands.toFixed(1)}K` } if (Math.abs(num) >= 1.0e3) { return `${thousands.toFixed(1)}K` } return num.toFixed(); }; console.log(numRound(-23400006)) console.log(numRound(1200)) console.log(numRound(654321)) console.log(numRound(12334000060))

使用str.lengthswitch case

var number=12345;

像這樣的東西

switch (number.length) {
  case 4:
    alert(number/10000+"Thousand");
    break;

}

這是對迪帕克·托馬斯的回答的改進。

它在必要時處理小數,並提供更多的可讀性和對函數的控制。

運行代碼片段以查看顯示其用法的 5 個警報實時演示。

 function numRound(num) { num = Math.abs(Number(num)) const billions = num/1.0e+9 const millions = num/1.0e+6 const thousands = num/1.0e+3 return num >= 1.0e+9 && billions >= 100 ? Math.round(billions) + "B" : num >= 1.0e+9 && billions >= 10 ? billions.toFixed(1) + "B" : num >= 1.0e+9 ? billions.toFixed(2) + "B" : num >= 1.0e+6 && millions >= 100 ? Math.round(millions) + "M" : num >= 1.0e+6 && millions >= 10 ? millions.toFixed(1) + "M" : num >= 1.0e+6 ? millions.toFixed(2) + "M" : num >= 1.0e+3 && thousands >= 100 ? Math.round(thousands) + "K" : num >= 1.0e+3 && thousands >= 10 ? thousands.toFixed(1) + "K" : num >= 1.0e+3 ? thousands.toFixed(2) + "K" : num.toFixed() } alert(numRound(1234)) alert(numRound(23456)) alert(numRound(345678)) alert(numRound(4567890)) alert(numRound(56789012))

實現此目的的最簡單方法是使用 Intl api。

例子:

 const formatter = Intl.NumberFormat('en', {notation: 'compact'}) const K = formatter.format(1555) // thousand const M = formatter.format(1555555) // million const B = formatter.format(1555555555) // billion const T = formatter.format(1555555555555) // trillion console.log(K, M, B, T)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM