Adding extra zeros in front of a number using jQuery?

后端 未结 14 1157
粉色の甜心
粉色の甜心 2020-11-30 02:45

I have file that are uploaded which are formatted like so

MR 1

MR 2

MR 100

MR 200

MR 300

相关标签:
14条回答
  • 2020-11-30 03:12

    Know this is an old post, but here's another short, effective way: zeros

    edit: dur. if num isn't string, you'd add:

    len -= String(num).length;

    else, it's all good

    function addLeadingZeros(sNum, len) {
        len -= sNum.length;
        while (len--) sNum = '0' + sNum;
        return sNum;
    }
    
    0 讨论(0)
  • 2020-11-30 03:13

    By adding 100 to the number, then run a substring function from index 1 to the last position in right.

    var dt = new Date();
    var month = (100 + dt.getMonth()+1).toString().substr(1, 2);
    var day = (100 + dt.getDate()).toString().substr(1, 2);
    
    console.log(month,day);
    

    you will got this result from the date of 2020-11-3

    11,03
    

    I hope the answer is useful

    0 讨论(0)
  • 2020-11-30 03:16

    str could be a number or a string.

    formatting("hi",3);
    function formatting(str,len)
    {
       return ("000000"+str).slice(-len);
    }
    

    Add more zeros if needs large digits

    0 讨论(0)
  • 2020-11-30 03:17

    I needed something like this myself the other day, Pud instead of always a 0, I wanted to be able to tell it what I wanted padded ing the front. Here's what I came up with for code:

    function lpad(n, e, d) {
      var o = ''; if(typeof(d) === 'undefined'){ d='0'; } if(typeof(e) === 'undefined'){ e=2; }
      if(n.length < e){ for(var r=0; r < e - n.length; r++){ o += d; } o += n; } else { o=n; }
      return o; }
    

    Where n is what you want padded, e is the power you want it padded to (number of characters long it should be), and d is what you want it to be padded with. Seems to work well for what I needed it for, but it would fail if "d" was more than one character long is some cases.

    0 讨论(0)
  • 2020-11-30 03:18

    If you split on the space, you can add leading zeros using a simple function like:

    function addZeros(n) {
      return (n < 10)? '00' + n : (n < 100)? '0' + n : '' + n;
    }
    

    So you can test the length of the string and if it's less than 6, split on the space, add zeros to the number, then join it back together.

    Or as a regular expression:

    function addZeros(s) {
      return s.replace(/ (\d$)/,' 00$1').replace(/ (\d\d)$/,' 0$1');
    }
    

    I'm sure someone can do it with one replace, not two.

    Edit - examples

    alert(addZeros('MR 3'));    // MR 003
    alert(addZeros('MR 23'));   // MR 023
    alert(addZeros('MR 123'));  // MR 123
    alert(addZeros('foo bar 23'));  // foo bar 023
    

    It will put one or two zeros infront of a number at the end of a string with a space in front of it. It doesn't care what bit before the space is.

    0 讨论(0)
  • 2020-11-30 03:18
    var str = "43215"; 
    console.log("Before : \n string :"+str+"\n Length :"+str.length);
    var max = 9;
    while(str.length < max ){
                                    str = "0" + str;
    
                            }
    console.log("After : \n string :"+str+"\n Length :"+str.length);
    

    It worked for me ! To increase the zeroes, update the 'max' variable

    Working Fiddle URL : Adding extra zeros in front of a number using jQuery?:

    0 讨论(0)
提交回复
热议问题