In this example we will show how to format a number with leading zeros using variations in java, guava and apache commons. Often you deal with inputs that are stored as numbers that you want you want to display as a formatted string. A couple examples are social security number (SSN) or a phone number. You could argue whether or not the raw data is a string but when you display it you want it to look a format that is natural to a user. In each snippet below we defined the length of the value to be five and we will pass 123 to be formatted as '00123'.
Straight up Java
Using java's DecimalFormat we will format a integer with leading zeros by creating a pattern and then calling the DecimalFormat.format passing it in the value to format.
Using java String.format method we will specify a format and then pass in the object to be formatted.
Using guava we will pad leading zeros to a decimal by calling the Strings.padStart utility method specifying the minimum value and the character to pad with.
Similar to the guava snippet above we will specify a min length and the value to pad with which will result in padding number with leading zeros.