【问题标题】:How do I round millions and thousands in JavaScript?如何在 JavaScript 中舍入数百万和数千?
【发布时间】:2012-10-05 16:55:01
【问题描述】:

我正在尝试对大数字进行舍入。例如,如果我有这个号码:

12,645,982

我想把这个数字四舍五入并显示为:

13 mil

或者,如果我有这个号码:

1,345

我想将其四舍五入并显示为:

1 thousand

如何在 JavaScript 或 jQuery 中做到这一点?

【问题讨论】:

  • 是的,我有一个很酷的答案,但它必须等到下一次

标签: javascript jquery rounding number-formatting shorthand


【解决方案1】:

这是一个格式化数千、数百万和数十亿的实用函数:

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,""))

参考文献

【讨论】:

  • 这很好用。我将它用作 AngularJS 过滤器:// 添加 M、B 并舍入大数字。例如5.3B, 6M, mm2.filter('bigNumRound', function() { return function (val) { // 十亿的九个零 return Math.abs(Number(val)) >= 1.0e+9 ? Math.abs( Number(val)) / 1.0e+9 + "B" // 六个零代表百万 : Math.abs(Number(val)) >= 1.0e+6 ? Math.abs(Number(val)) / 1.0e+ 6 + "M" // 千位的三个零 : Math.abs(Number(val)) >= 1.0e+3 ? Math.abs(Number(val)) / 1.0e+3 + "K" : Math.abs (数字(val)); } });在 HTML 中:{{someval | bigNumRound}}
  • 谢谢保罗。它帮助了我..我正在添加一个小提琴,可以帮助其他人看到它的工作。 jsfiddle.net/ykng4tkL
  • 无三元:function MoneyFormat(labelValue) { // Nine Zeroes for Billions if (Math.abs(Number(labelValue)) >= 1.0e+9) { return `R$ ${Math.trunc(Math.abs(Number(labelValue)) / 1.0e+9)}B`; } // Six Zeroes for Millions if (Math.abs(Number(labelValue)) >= 1.0e+6) { return `R$ ${Math.trunc(Math.abs(Number(labelValue)) / 1.0e+6)}M`; } // Three Zeroes for Thousands if (Math.abs(Number(labelValue)) >= 1.0e+3) { return `R$ ${Math.trunc(Math.abs(Number(labelValue)) / 1.0e+3)}K`; } return Math.abs(Number(labelValue)); }
【解决方案2】:

数字 JS .. 如果有人检查这个,请检查数字 Js。您只需要包含脚本,然后只包含一行代码

numeral(yourNumber).format('0.0a')

【讨论】:

  • 我的朋友们,这才是真正的答案
【解决方案3】:
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/

【讨论】:

    【解决方案4】:

    @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)); 
    }
    

    【讨论】:

    • 这好多了...如果能够选择多少“小数”就好了 喜欢而不是 12m 会很好 有 12.1m 或者不是 12k 会很好12.1k 等等等等
    • @iiiml0sto1 我对这个很好的示例进行了一些更改,以便在必要时也处理小数。见:stackoverflow.com/a/68273755/8296887
    【解决方案5】:
    var number = 1345;
    var zeroCount = 3;
    var roundedNumber = Math.round( number / Math.pow(10,zeroCount) )
    

    只需将 zeroCount 调整为 3 表示数千,6 表示数百万等。我假设您可以使用 if 语句并且只需要一些数学函数的帮助。

    【讨论】:

      【解决方案6】:

      Paul Sweatte 的回答很好

      Deepak Thomas 的回答很好

      我的答案是更灵活/可定制且更易于阅读,所以这里是:

      代码

      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',
        },
      }));

      使用上面的代码,您可以控制十亿、百万或数千的小数位数,您还可以控制要显示的单位:)

      【讨论】:

        【解决方案7】:

        使用str.lengthswitch 的情况

        var number=12345;
        

        类似的东西

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

        【讨论】:

        • 在这种情况下,我认为是这样,因为看起来这个问题的创建者没有尝试任何事情来实现这一目标。
        【解决方案8】:

        这是对 Deepak Thomas 的回答的改进。

        它会在必要时处理小数,并提供更多的可读性和对函数的控制。

        运行代码 sn-p 以查看 5 alert 现场演示,展示其用法。

        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))

        【讨论】:

          猜你喜欢
          • 2023-04-05
          • 1970-01-01
          • 2021-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多