• <small id='FTaz7'></small><noframes id='FTaz7'>

  • <i id='FTaz7'><tr id='FTaz7'><dt id='FTaz7'><q id='FTaz7'><span id='FTaz7'><b id='FTaz7'><form id='FTaz7'><ins id='FTaz7'></ins><ul id='FTaz7'></ul><sub id='FTaz7'></sub></form><legend id='FTaz7'></legend><bdo id='FTaz7'><pre id='FTaz7'><center id='FTaz7'></center></pre></bdo></b><th id='FTaz7'></th></span></q></dt></tr></i><div id='FTaz7'><tfoot id='FTaz7'></tfoot><dl id='FTaz7'><fieldset id='FTaz7'></fieldset></dl></div>
    <legend id='FTaz7'><style id='FTaz7'><dir id='FTaz7'><q id='FTaz7'></q></dir></style></legend>

        <bdo id='FTaz7'></bdo><ul id='FTaz7'></ul>
      <tfoot id='FTaz7'></tfoot>

        使用 1.5K、1M、1G 等公制前缀格式化 javascript 数字

        时间:2023-05-30

          <small id='6Bv10'></small><noframes id='6Bv10'>

          <i id='6Bv10'><tr id='6Bv10'><dt id='6Bv10'><q id='6Bv10'><span id='6Bv10'><b id='6Bv10'><form id='6Bv10'><ins id='6Bv10'></ins><ul id='6Bv10'></ul><sub id='6Bv10'></sub></form><legend id='6Bv10'></legend><bdo id='6Bv10'><pre id='6Bv10'><center id='6Bv10'></center></pre></bdo></b><th id='6Bv10'></th></span></q></dt></tr></i><div id='6Bv10'><tfoot id='6Bv10'></tfoot><dl id='6Bv10'><fieldset id='6Bv10'></fieldset></dl></div>
        • <legend id='6Bv10'><style id='6Bv10'><dir id='6Bv10'><q id='6Bv10'></q></dir></style></legend>

          <tfoot id='6Bv10'></tfoot>

            • <bdo id='6Bv10'></bdo><ul id='6Bv10'></ul>
                <tbody id='6Bv10'></tbody>

                1. 本文介绍了使用 1.5K、1M、1G 等公制前缀格式化 javascript 数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想使用 Metric Prefix 显示数字,其中包含 3 个有效数字,如下所示:

                  I want to display numbers using a Metric Prefix with 3 significant digits like so:

                  1 shows as 1
                  999 shows as 999
                  1000 shows as 1K
                  999000 shows as 999K
                  1000000 shows as 1M
                  1500000 shows as 1.5M
                  1000000000 shows as 1G
                  etc...
                  

                  我可以编写自己的 javascript 函数来执行此操作,但我想知道是否有一种标准的方式来格式化这样的数字?

                  I could write my own javascript function to do this but I was wondering if there is a standard way of formatting numbers like this?

                  推荐答案

                  您可以将范围放在对象数组中,然后循环遍历它以格式化数字:

                  You can put the ranges in an array of objects, and just loop through it to format the number:

                  var ranges = [
                    { divider: 1e18 , suffix: 'E' },
                    { divider: 1e15 , suffix: 'P' },
                    { divider: 1e12 , suffix: 'T' },
                    { divider: 1e9 , suffix: 'G' },
                    { divider: 1e6 , suffix: 'M' },
                    { divider: 1e3 , suffix: 'k' }
                  ];
                  
                  function formatNumber(n) {
                    for (var i = 0; i < ranges.length; i++) {
                      if (n >= ranges[i].divider) {
                        return (n / ranges[i].divider).toString() + ranges[i].suffix;
                      }
                    }
                    return n.toString();
                  }
                  

                  使用示例:

                  var s = formatNumber(999000);
                  

                  要处理负数,您可以先在函数中添加:

                  To also handle negative numbers, you would add this first in the function:

                    if (n < 0) {
                      return '-' + formatNumber(-n);
                    }
                  

                  这篇关于使用 1.5K、1M、1G 等公制前缀格式化 javascript 数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:并非所有浏览器都支持 toLocaleString()? 下一篇:Number().toLocaleString() 在不同的浏览器中有不同的格

                  相关文章

                  最新文章

                  <i id='0LEBI'><tr id='0LEBI'><dt id='0LEBI'><q id='0LEBI'><span id='0LEBI'><b id='0LEBI'><form id='0LEBI'><ins id='0LEBI'></ins><ul id='0LEBI'></ul><sub id='0LEBI'></sub></form><legend id='0LEBI'></legend><bdo id='0LEBI'><pre id='0LEBI'><center id='0LEBI'></center></pre></bdo></b><th id='0LEBI'></th></span></q></dt></tr></i><div id='0LEBI'><tfoot id='0LEBI'></tfoot><dl id='0LEBI'><fieldset id='0LEBI'></fieldset></dl></div>

                  <small id='0LEBI'></small><noframes id='0LEBI'>

                  <tfoot id='0LEBI'></tfoot>

                2. <legend id='0LEBI'><style id='0LEBI'><dir id='0LEBI'><q id='0LEBI'></q></dir></style></legend>

                    • <bdo id='0LEBI'></bdo><ul id='0LEBI'></ul>