Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created July 22, 2014 14:39
Show Gist options
  • Save jikeytang/c317415fdc6bab637932 to your computer and use it in GitHub Desktop.
Save jikeytang/c317415fdc6bab637932 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140723-题目1
如何给数字补充固定位数前导0?
比如3前面补3位0,结果为0003。
4前面补2位0,结果为004。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@jikeytang
Copy link
Author

// from 轨道哥

function cc(num,count){
    var i=1;
    count=i<<count;
    count=count.toString(2).slice(1);
    return count+num.toString()
}
console.log(cc(3,5));

@xianlaioy
Copy link

/** 功能:将字符串(或数字)变为特定长度的串
参数说明:src 需要处理的变量
          len 变换的长度
          padChar 不足长度所补的字符(补在字符串的前面)
*/
function pad(src,len,padChar)
{
    src = "" + src;
    if(!padChar) padChar = "0";
    var tempStr = "";
    for(var i = src.length ; i < len ; i++){
        tempStr += padChar ;
    }
    return tempStr + src;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment