Created
March 19, 2016 09:02
-
-
Save imzhi/2a2e498fe81fb45db623 to your computer and use it in GitHub Desktop.
补齐0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var pad_str = (function(arr) { // 数组缓存补齐的0个数,空间换时间 <http://www.cnblogs.com/bluedream2009/archive/2009/09/08/1562910.html> | |
| // num 要补0的数字 n 需补齐到多少位 | |
| return function(num, n) { | |
| var pad_len = (n - ('' + num).length); | |
| return pad_len > 0 ? (arr[pad_len] || ( arr[pad_len] = Array(pad_len+1).join('0') )) + num | |
| : num; | |
| }; | |
| })([]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment