久久久久久久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 中用前導(dǎo)零填充數(shù)字

        Pad a number with leading zeros in JavaScript(在 JavaScript 中用前導(dǎo)零填充數(shù)字)
        <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 中用前導(dǎo)零填充數(shù)字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時送ChatGPT賬號..

                  在 JavaScript 中,我需要有填充.

                  In JavaScript, I need to have padding.

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

                  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.

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

                  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更新

                  您可以使用內(nèi)置的 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;
                  }
                  

                  當您使用數(shù)字初始化數(shù)組時,它會創(chuàng)建一個數(shù)組,并將 length 設(shè)置為該值,以便該數(shù)組看起來包含那么多 undefined 元素.盡管一些 Array 實例方法會跳過沒有值的數(shù)組元素,但 .join() 不會,或者至少不完全;它將它們視為它們的值是空字符串.因此,您會在每個數(shù)組元素之間獲得零字符(或任何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
                  

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

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

                  相關(guān)文檔推薦

                  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 以使其以不同的方式運行任務(wù)?)
                  Why do we need to install gulp globally and locally?(為什么我們需要在全局和本地安裝 gulp?)
                  How to run Gulp tasks sequentially one after the other(如何一個接一個地依次運行 Gulp 任務(wù))
                  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>
                            主站蜘蛛池模板: 午夜精品久久久久久久久久久久 | 综合中文字幕 | 国产激情精品一区二区三区 | 成年人精品视频 | 中文字幕免费视频 | 黄片毛片在线观看 | 全部免费毛片在线播放网站 | 午夜精品一区二区三区在线视频 | 欧美激情久久久久久 | 久久久亚洲一区 | 精品久久久久久18免费网站 | 日本精品国产 | 可以免费观看的av片 | 黄视频免费观看 | 日韩精品免费视频 | 日韩精品在线一区 | 密桃av | 国产精品亚洲视频 | www久| 久久精品超碰 | 日韩欧美在线免费观看 | 亚洲一区影院 | 亚洲免费成人 | 久久毛片 | 福利网址 | 在线第一页 | 欧美日韩最新 | 国产99久久精品一区二区永久免费 | 一区二区三区四区国产 | 日干夜干 | 国产91亚洲精品一区二区三区 | 日日操视频 | 亚洲伊人久久综合 | 国产精品久久久久久久久免费高清 | 美女福利网站 | 一区二区三区精品视频 | 成人在线h| 国产精品99久久久久久久vr | 欧美一级淫片免费视频黄 | 一级黄在线观看 | 国产色播av在线 |