Sub topic 9
Text format and leading zeros
This chapter assumes you can enter data in cells, write a basic formula such as =A1+B1, and apply formats from the Ribbon. It also assumes you know that a number format changes how a value looks, not what is stored. It covers identifiers such as ZIP codes, product codes, and account numbers, which look like numbers but behave like labels.
Why leading zeros disappear
Excel treats anything it can read as a number as a number, and a number has no leading zeros. If you type 00042 into a General cell, Excel stores 42 and displays 42. The zeros are gone from the stored value, so no format can bring them back.
This is a problem for identifiers. A US ZIP code such as 02134 becomes 2134. An employee ID such as 007 becomes 7. You can't do arithmetic with these values in any useful way, so they should not be stored as numbers.
You have two options:
- Store the value as text, which keeps every character exactly as typed.
- Keep a number and use a custom format that pads it for display.
The Text format
The Text format, shown as @ in the format list, tells Excel to treat whatever you enter as a string of characters. To apply it:
- Select the cells before you enter any data.
- Press Ctrl+1 to open Format Cells.
- On the Number tab, choose Text and click OK.
Now 00042 stays 00042. Text is left-aligned by default, and Excel may show a small green triangle in the corner of the cell. The triangle's tooltip reads "Number stored as text".
You can also type an apostrophe before the value, as in '00042. The apostrophe is not part of the cell's content. It appears in the formula bar but not in the cell, and it marks that single entry as text without changing the cell's format.
The order matters
Applying Text to cells that already hold numbers does not convert them. The cell holding 42 still holds the number 42 and only the format label changes. Excel converts a value to text only when you enter or re-enter it after the format is set. To convert existing cells, set the Text format, then re-enter each value by pressing F2 and then Enter. For a whole column, Data > Text to Columns (finish on the last step with the column set to Text) does this in one pass.
Even then, the zeros that were already lost are not recovered. You would have to retype them or rebuild them with a formula.
Padding with a custom format
If every identifier has the same length, a custom format can restore the zeros for display. The cell stays numeric, so sorting and arithmetic still work.
00000Each 0 is a digit placeholder that shows a zero when no digit is available. With this format, 42 displays as 00042, but the stored value is still 42. The formula bar shows 42. A number with more than five digits shows all of its digits. The format never truncates.
Excel's built-in Special category has ready-made versions of this idea: Zip Code, Zip Code + 4, Phone Number, and Social Security Number. Each is a custom format underneath. A Social Security Number format, for example, is 000-00-0000.
Choosing between text and padded numbers
| Question | Text format | Custom format `00000` |
|---|---|---|
| What is stored? | The characters typed, such as `"00042"` | The number `42` |
| Does it survive copying into another program? | Yes, as `00042` | Usually no. Pasted as plain text, it arrives as `42` |
| Can the length vary? | Yes | No. The pad width is fixed in the format |
| Does SUM include it? | No, text is ignored | Yes |
| Sorts as | Text (`"10"` before `"9"`) | Number (9 before 10) |
Use text when the value is a label that is never added or averaged, such as an ID, a phone number, or a part number with letters. Use the padded number format when the value is a true number that needs to look uniform and you may still calculate with it.
Building padded text with formulas
When data arrives as numbers and you need real text with zeros, build it with a formula. The TEXT function converts a number to a string using a format code:
=TEXT(A2,"00000")The result is the text "00042". An older approach that does not depend on format codes joins zeros to the front and keeps the right-hand characters:
=RIGHT("00000"&A2,5)Both return text, so the result is left-aligned and SUM ignores it. If A2 holds a number longer than five digits, RIGHT cuts off its leading digits, whereas TEXT keeps them. Check your longest value before you choose.
To go the other way, =VALUE("00042") returns the number 42. Converting text back to numbers is covered in a later chapter.
Long numbers lose digits
Excel keeps only 15 significant digits in a number. Anything past the fifteenth becomes a zero. A 16-digit credit card number typed into a General cell is changed to something like 4.1111E+15, and the last digit is stored as 0. A custom format cannot fix this, because the digits were lost when the value was stored. The cell must be formatted as Text before entry. This applies to credit card numbers, long tracking numbers, and similar identifiers.
Importing data
Opening a CSV file by double-clicking it makes Excel guess the type of each column, and it drops leading zeros as it does so. To keep them:
- Use Data > From Text/CSV and choose the file.
- Select Transform Data, or the data type options in the preview.
- Set the identifier column to Text before loading.
Setting the type at import is more reliable than trying to repair the data afterwards.
Choosing a method
Common problems
- Zeros still missing after applying Text. The values were already numbers. Re-enter them or rebuild them with
TEXT. - Lookups return #N/A. One table has the key as text
"00042"and the other as the number42. A lookup treats these as different values. Convert both sides to the same type. - A column of numbers sums to zero. The numbers are stored as text. Convert them with
VALUE, Text to Columns, or by multiplying by 1 in a helper column. - Sorting looks wrong. Text sorts character by character, so
"100"comes before"20". Pad the values to equal length, or store them as numbers.
Exercise
Cell A2 contains the number 7, and you need the text "00007" in B2 so that it can be used as a product code. Which formula do you write? What would happen to the stored value if you instead applied the custom format 00000 to A2?
Show answer
In B2, write =TEXT(A2,"00000"), or =RIGHT("00000"&A2,5). Either returns the text "00007". Applying the custom format 00000 to A2 changes only its display to 00007. The stored value remains the number 7, so a copy pasted as plain text elsewhere would arrive as 7.
Exercise
You type the 16-digit number 1234567890123456 into a General cell and the cell shows 1.23457E+15. After changing the format to Text, the cell still shows the same thing. Why, and what should you do?
Show answer
The value was already stored as a number, and Excel keeps only 15 significant digits, so the sixteenth digit was replaced with a zero. Changing the format afterwards cannot recover it. Format the cell as Text before entering the number (or type an apostrophe first), then enter the full 16 digits again.