All files / num fixTo.js

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19                        10x 10x 10x     2x  
/**
 * 修正补位
 * @method num/fixTo
 * @param {Number|String} num 要补位的数字
 * @param {Number} [w=2] w 补位数量
 * @return {String} 经过补位的字符串
 * @example
 * var $fixTo = require('@spore-ui/kit/packages/num/fixTo');
 * $fixTo(0, 2); //return '00'
 */
 
function fixTo(num, w) {
  var str = num.toString();
  w = Math.max((w || 2) - str.length + 1, 0);
  return new Array(w).join('0') + str;
}
 
module.exports = fixTo;