Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created August 3, 2014 14:04
Show Gist options
  • Save jikeytang/3026a06722701f008570 to your computer and use it in GitHub Desktop.
Save jikeytang/3026a06722701f008570 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140804-题目1
如何给一个长度一定的数字补0.
比如:1,需要长度是5位,那么是 00001。
23,需要长度是5位,那么是 00023。
234,需要长度是5位,那么是 00234。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@rambo-panda
Copy link

function pad(num, length) {
    var _length = length - (num+'').split('').length;

    return _length > 0 &&  parseInt(length) === length ?
        new Array(_length).join('0') + num :
        num;
}

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