久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='AGBSW'></bdo><ul id='AGBSW'></ul>

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

      <tfoot id='AGBSW'></tfoot>

        在 JavaScript 中用前導零填充數字

        Pad a number with leading zeros in JavaScript(在 JavaScript 中用前導零填充數字)
        <tfoot id='J9AVX'></tfoot>
          <bdo id='J9AVX'></bdo><ul id='J9AVX'></ul>

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

                    <tbody id='J9AVX'></tbody>
                  <legend id='J9AVX'><style id='J9AVX'><dir id='J9AVX'><q id='J9AVX'></q></dir></style></legend>
                  <i id='J9AVX'><tr id='J9AVX'><dt id='J9AVX'><q id='J9AVX'><span id='J9AVX'><b id='J9AVX'><form id='J9AVX'><ins id='J9AVX'></ins><ul id='J9AVX'></ul><sub id='J9AVX'></sub></form><legend id='J9AVX'></legend><bdo id='J9AVX'><pre id='J9AVX'><center id='J9AVX'></center></pre></bdo></b><th id='J9AVX'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='J9AVX'><tfoot id='J9AVX'></tfoot><dl id='J9AVX'><fieldset id='J9AVX'></fieldset></dl></div>

                  本文介紹了在 JavaScript 中用前導零填充數字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  在 JavaScript 中,我需要有填充.

                  In JavaScript, I need to have padding.

                  例如,如果我有數字 9,它將是0009".如果我有一個數字,比如 10,它將是0010".注意它總是包含四位數字.

                  For example, if I have the number 9, it will be "0009". If I have a number of say 10, it will be "0010". Notice how it will always contain four digits.

                  執行此操作的一種方法是將數字減去 4 以獲得我需要輸入的 0 的數量.

                  One way to do this would be to subtract the number minus 4 to get the number of 0s I need to put.

                  有沒有更巧妙的方法來做到這一點?

                  Is there was a slicker way of doing this?

                  推薦答案

                  ES2017更新

                  您可以使用內置的 String.prototype.padStart()

                  n = 9;
                  String(n).padStart(4, '0'); // '0009'
                  
                  n = 10;
                  String(n).padStart(4, '0'); // '0010'
                  


                  沒有很多花哨"的東西.目前為止:


                  Not a lot of "slick" going on so far:

                  function pad(n, width, z) {
                    z = z || '0';
                    n = n + '';
                    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
                  }
                  

                  當您使用數字初始化數組時,它會創建一個數組,并將 length 設置為該值,以便該數組看起來包含那么多 undefined 元素.盡管一些 Array 實例方法會跳過沒有值的數組元素,但 .join() 不會,或者至少不完全;它將它們視為它們的值是空字符串.因此,您會在每個數組元素之間獲得零字符(或任何z")的副本;這就是為什么里面有一個+ 1.

                  When you initialize an array with a number, it creates an array with the length set to that value so that the array appears to contain that many undefined elements. Though some Array instance methods skip array elements without values, .join() doesn't, or at least not completely; it treats them as if their value is the empty string. Thus you get a copy of the zero character (or whatever "z" is) between each of the array elements; that's why there's a + 1 in there.

                  示例用法:

                  pad(10, 4);      // 0010
                  pad(9, 4);       // 0009
                  pad(123, 4);     // 0123
                  
                  pad(10, 4, '-'); // --10
                  

                  這篇關于在 JavaScript 中用前導零填充數字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browserify, Babel 6, Gulp - Unexpected token on spread operator(Browserify,Babel 6,Gulp - 傳播運算符上的意外令牌)
                  Is it possible to pass a flag to Gulp to have it run tasks in different ways?(是否可以將標志傳遞給 Gulp 以使其以不同的方式運行任務?)
                  Why do we need to install gulp globally and locally?(為什么我們需要在全局和本地安裝 gulp?)
                  How to run Gulp tasks sequentially one after the other(如何一個接一個地依次運行 Gulp 任務)
                  Visual Studio 2015 crashes when opening Javascript files(打開 Javascript 文件時 Visual Studio 2015 崩潰)
                  Detect FLASH plugin crashes(檢測 FLASH 插件崩潰)
                  <i id='2QgmB'><tr id='2QgmB'><dt id='2QgmB'><q id='2QgmB'><span id='2QgmB'><b id='2QgmB'><form id='2QgmB'><ins id='2QgmB'></ins><ul id='2QgmB'></ul><sub id='2QgmB'></sub></form><legend id='2QgmB'></legend><bdo id='2QgmB'><pre id='2QgmB'><center id='2QgmB'></center></pre></bdo></b><th id='2QgmB'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='2QgmB'><tfoot id='2QgmB'></tfoot><dl id='2QgmB'><fieldset id='2QgmB'></fieldset></dl></div>
                1. <legend id='2QgmB'><style id='2QgmB'><dir id='2QgmB'><q id='2QgmB'></q></dir></style></legend>
                2. <tfoot id='2QgmB'></tfoot>

                  1. <small id='2QgmB'></small><noframes id='2QgmB'>

                        • <bdo id='2QgmB'></bdo><ul id='2QgmB'></ul>
                            <tbody id='2QgmB'></tbody>
                            主站蜘蛛池模板: 美女久久久久久久 | 成人欧美一区二区三区 | 欧美高清成人 | 日韩精品一区二区三区中文字幕 | 91极品欧美视频 | 久久lu| 亚洲网站在线观看 | 美女视频一区二区三区 | 欧洲尺码日本国产精品 | 日本免费视频在线观看 | 99精品免费视频 | 欧美福利三区 | 久久综合久久久 | 欧美国产精品一区二区 | 中文字幕一区在线 | 91看片免费版| 99re热这里只有精品视频 | a级毛片基地 | 免费看国产一级特黄aaaa大片 | 亚洲精品在线免费看 | 一级片av | 国产精品2区 | 国产一区二区黑人欧美xxxx | 亚洲精品在线看 | 国产精品久久久久久久久久久久久久 | 国产精品视频久久 | 免费黄色av网站 | 99成人| 成人免费视屏 | 欧美一级欧美三级在线观看 | 亚洲成人久久久 | 欧美精品在线免费观看 | 欧美综合在线观看 | 亚洲精品久久久久久久久久久 | 国产精品视频在线观看 | 精品国产一区二区国模嫣然 | 人人射人人 | 欧美精品网站 | 亚洲欧美日韩在线 | 毛片av免费看 | 亚洲一区二区久久久 |