Friday, July 16, 2021

Apex - solution to add leading zeros

 function pad(

  x, // input number to pad
  y // input number for trunc
){
  return (
    1e16 + x + "" // concatenate a big number to the input number and converting to string
  ).slice(-y) // trunc last Y numbers
}

Output

pad(1,2) => 01
pad(123,2) => 23
pad(123,5) => 00123

No comments:

Post a Comment