Skip to content

Dave Helgerson

Salesforce Software Development and Consulting

Menu
  • Contact
  • Resume
Menu

Pad Number with Leading Zeros

October 1, 2013October 1, 2013

Left pad a number with leading zeroes

Thanks to mauricekremer.dyndns.org for the concept and code. Below is my take on the function.

public static String PadZeros(Integer Num, Integer Len) {
  String s = String.valueOf(Num);
  while (s.length() < Len) s = '0' + s;
  return s;
}

Related

2 thoughts on “Pad Number with Leading Zeros”

  1. Oleksiy says:
    June 17, 2014 at 5:06 pm

    s.leftPad(Len).replace(‘ ‘,’0’); should do the same

    1. daveh says:
      July 8, 2014 at 1:46 am

      This is great. Simpler and doesn’t require a loop.

Comments are closed.

Super Clone Pro
© 2023 Dave Helgerson | Powered by Minimalist Blog WordPress Theme