Sub topic 9
Text format and leading zeros
This chapter assumes you can enter data in cells, write a basic formula, and open Format Cells. It builds on the idea that a cell has a stored value and a separate display format.
Why leading zeros disappear
Excel treats anything that looks like a number as a number. A number has no leading zeros, so typing 0042 stores 42. ZIP codes, product codes, and employee IDs often start with zeros, so they lose digits as soon as you type them.
You have two ways to keep the zeros. The right choice depends on whether the stored value must keep its zeros.
| Approach | Stored value | Displayed value | Good for |
|---|---|---|---|
| Custom format `00000` | 42 (a number) | 00042 | Codes you still calculate with or sort numerically |
| Text format | "00042" (text) | 00042 | Identifiers that are never added or averaged |
Option 1: a custom format that pads with zeros
Select the cells, open Format Cells, choose Custom, and enter this code:
00000Each 0 is a placeholder that always shows a digit. If the number has fewer digits than the code, Excel fills the left side with zeros. The number 42 displays as 00042, but the cell still holds 42. A formula such as =A1+1 returns 43.
Numbers longer than five digits are not cut off. The code only sets a minimum width.
Option 2: the Text format
Choose Text in the Number Format list before you type the value. Excel then stores what you type as text and keeps every character. Text is left-aligned by default, and many cells show a small green triangle that warns about a "number stored as text".
Two details matter here:
- The format must be applied first. If you type
00042into a General cell and then switch to Text, the zeros are already gone. Retype the value. - A leading apostrophe, as in
'00042, gives the same result for a single cell. The apostrophe is not part of the value.
flowchart TDDo not rely on the diagram above. Use the decision rules below instead.
Choosing between them
- Will you do math on the values? Use the custom format.
- Must the value stay exactly as typed, including varying lengths or letters? Use Text.
- Will the data be imported or exported? Text is safer, because other programs then receive the full string.
Text format and formulas
A formula cell formatted as Text shows its formula instead of calculating. If you see =A1+B1 in a cell where you expected a result, change the format to General, then press F2 and Enter to re-enter the formula.
Text values also compare differently from numbers. In a lookup, the text "42" does not match the number 42, so keep both sides of a match in the same type.
Exercise
A column of ZIP codes must show five digits, such as 02134, and you plan to sort it numerically. Which approach do you use, and what do you enter?
Show answer
Use a custom number format with the code 00000. The cells keep numeric values such as 2134, which sort numerically, and display as 02134.