Add Leading Zeros Formula

Spreadsheets treat 00123 as the number 123 and drop the zeros. The TEXT function pads a number to a fixed width and returns it as text, which is what you need for ZIP codes, product codes, and IDs.

Free, no sign-up required · 5 free generations a day

Last updated

Describe your task, get the formula

Type what you need in plain English, or pick an example below. You get the formula, a step-by-step explanation, and a worked example.

Leading zero formulas

Syntax
=TEXT(A2, "00000")
=REPT("0", 5 - LEN(A2)) & A2
="ID-" & TEXT(A2, "0000")

In a TEXT format, each 0 is a digit that is always shown, so "00000" pads any number to five digits. REPT repeats a character, which lets you build the padding yourself and also works on values that are already text.

Examples

Pad a number to five digits

=TEXT(A2,"00000")

Turns 42 into 00042 and 12345 into 12345. Use it for ZIP codes and fixed-length numeric codes.

Build a prefixed ID

="ID-"&TEXT(A2,"0000")

Joins a text prefix to a four-digit padded number, so 7 becomes ID-0007.

Pad text to a fixed length

=REPT("0",8-LEN(A2))&A2

Adds as many zeros as needed to reach eight characters. It works for text values such as codes that contain letters.

Common mistakes and how to fix them

The zeros disappear after you copy the values

TEXT returns text, but pasting into a cell formatted as General can turn it back into a number. Format the destination column as Text before pasting.

#VALUE! with REPT

If the value is already longer than the target length, 8-LEN(A2) is negative and REPT fails. Wrap it: =REPT("0",MAX(0,8-LEN(A2)))&A2.

A custom number format shows zeros but the cell is still a number

Formatting only changes the display. Exports, lookups, and joined text use the real value without zeros, so use TEXT when the zeros must be part of the data.

Frequently asked questions

How do I keep leading zeros when opening a CSV file?

Import it with the Data, From Text/CSV wizard and set the column type to Text. Opening the file directly makes Excel convert the column to numbers and drop the zeros.

Can I just type an apostrophe?

Yes, typing '00123 keeps the zeros as text for a single cell. It is fine for a few entries, but a formula is better for a whole column.

Does it work in Google Sheets?

Yes. TEXT, REPT, and LEN use the same syntax in Google Sheets.

Don't want to write it by hand?

Describe what you need in plain English and get a ready-to-use formula in seconds.

Try the formula generator