JAVASCRIPT   12

zeroFill

Guest on 1st May 2022 04:32:14 PM

  1. export default function zeroFill(number, targetLength, forceSign) {
  2.     var absNumber = '' + Math.abs(number),
  3.         zerosToFill = targetLength - absNumber.length,
  4.         sign = number >= 0;
  5.     return (
  6.         (sign ? (forceSign ? '+' : '') : '-') +
  7.         Math.pow(10, Math.max(0, zerosToFill)).toString().substr(1) +
  8.         absNumber
  9.     );
  10. }

Raw Paste


Login or Register to edit or fork this paste. It's free.