Summary

To pad a number with a variable number of zeros, you can use a formula based on the TEXT function and the REPT function. In the example shown, the formula in D5 is:

=TEXT(B5,REPT("0",C5))

As the formula is copied down, the numbers in column B are padded with a progressively larger number of zeros based on the number in column C. Note that the result is a text value.

Generic formula

=TEXT(A1,REPT("0",n))

Explanation 

In this example, the goal is to pad a number with zeros. To illustrate how Excel functions can be combined, the number of zeros to use is variable and comes from column C. The formula used to solve this problem combines the TEXT function and the REPT function.

Fixed number

The TEXT function returns a number formatted as text, using the number format provided. The TEXT function can apply number formats of any kind to numbers. It is most often used when you want to maintain number formatting for a number when concatenating that number with other text. For example to use the TEXT function to pad a number 3, 4, and 5 zeros:

=TEXT(17,"000") // returns "017"
=TEXT(17,"0000") // returns "0017"
=TEXT(17,"00000") // returns "00017"

Notice the number format works by adding zeros to the left as needed to reach the total number of zeros supplied.

Variable number

To allow a variable number of zeros based on a number in another cell, we can add the REPT function into the mix. The REPT function simply repeats a text string a given number of times:

=REPT("a",2) // returns "aa"
=REPT("a",3) // returns "aaa"
=REPT("a",4) // returns "aaaa"

So to pad a number with 5 zeros, we can use REPT to assemble the five zeros into  the text"00000":

=TEXT(17,REPT("0",5)) // returns "00017"

The formula used in the example shown is simply a variation of the above formula. The formula in E9 evaluates like this:

=TEXT(B9,REPT("0",C9))
=TEXT(29,REPT("0",5))
=TEXT(29,"00000")
="00029"

Other characters

You can adapt this formula to use any character you like:

=TEXT(29,REPT("*",5)) // returns "***29"
=TEXT(29,REPT("-",5)) // returns "---29"

Pad for display only

Padding a number with zeros with the TEXT function changes the number into text, which may not suit your needs. To simply display a number with padding, you can use a regular number format. For example, to pad a number with 5 zeros for display only, select the cells and use the shortcut Control + 1 to open the Format Cells window.  Then:

Format cells > Number > Custom > "00000"

With this approach, the number is not converted to text but remains a true number. More details here.

Dave Bruns Profile Picture

AuthorMicrosoft Most Valuable Professional Award

Dave Bruns

Hi - I'm Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.