This tutorial shows different ways to add leading zeros in Excel: how to keep zeros as you type, show leading zeros in cells, remove or hide zeros.
If you use Excel not only to calculate numbers, but also maintain records such as zip codes, security numbers or employee ids, you may need to keep leading zeros in cells. However if you try typing a zip code like "00123" in a cell, Excel will immediately truncate it to "123".
The point is that Microsoft Excel treats postal codes, phone numbers and other similar entries as numbers, applies the General or Number format to them, and automatically removes preceding zeroes. Luckily, Excel also provides the means to keep leading zeros in cells, and further on in this tutorial you will find a handful of ways to do it.
For starters, let's see how you can put 0 in front of a number in Excel, for example type 01 in a cell. For this, simply change the cell format to Text:
As soon as you type a zero(s) before number, Excel will display a small green triangle in the top-left corner of the cell indicating that something is wrong with the cell contents. To remove that error indicator, select the cell(s), click the warning sign, and then click Ignore Error.
The following screenshot shows the result:
Another way to keep leading zeros in Excel is to prefix a number with an apostrophe ('). For example, instead of typing 01, type '01. In this case, you don't need to change the cell's format.
Bottom line: This simple technique has a significant limitation - the resulting value is a text string, not number, and consequently it cannot be used in calculations and numeric formulas. If that is not what you want, then change only the visual representation of the value by applying a custom number format as demonstrated in the next example.
To display leading zeroes, apply a custom number format by performing these steps:
In most cases, you will need a format code consisting of 0 placeholders, like 00. The number of zeros in the format code corresponds to the total number of digits you want to show in a cell (you will find a few examples below).
For example, to add leading zeros to create a 5-digit number, use the following format code: 00000
By using Excel custom numbers formats, you can add leading zeros to create fixed-length numbers, like in the above example, and variable-length numbers. It all boils down to which placeholder you use in the format code:
For example, if you apply the 000# format to some cell, any number you type in that cell will have up to 3 leading zeros.
Your custom number formats can also include spaces, hyphens, parentheses, etc. The detailed explanation can be found here: Custom Excel number format.
The following spreadsheet gives a few more examples of custom formats that can show leading zeros in Excel.
A | B | C | |
---|---|---|---|
1 | Custom format | Typed number | Displayed number |
2 | 00000 | 123 | 00123 |
3 | 000# | 123 | 0123 |
4 | 00-00 | 1 | 00-01 |
5 | 00-# | 1 | 00-1 |
6 | 000-0000 | 123456 | 012-3456 |
7 | ###-#### | 123456 | 12-3456 |
And the following format codes can be used to display numbers in special formats such us zip codes, phone numbers, credit card numbers, and social security numbers.
A | B | C | D | |
---|---|---|---|---|
1 | Custom format | Typed number | Displayed number | |
2 | Zip code | 00000 | 1234 | 01234 |
3 | Social security | 000-00-0000 | 12345678 | 012-34-5678 |
4 | Credit card | 0000-0000-0000-0000 | 12345556789123 | 0012-3455-5678-9123 |
5 | Phone numbers | 00-0-000-000-0000 | 12345556789 | 00-1-234-555-6789 |
Tip. Excel has a few predefined Special formats for postal codes, telephone numbers and social security numbers, as shown in the screenshot below:
Bottom line: This method is best to be used in situations when you work with a numeric dataset and the results should be numbers, not text. It changes only the display of a number, but not the number itself: leading zeros show up in cells, the actual value displays in the formula bar. When you reference such cells in formulas, the calculations are perfumed with the original values. Custom formats can only be applied to numeric data (numbers and dates) and the result is also a number or date.
While a custom number format shows zero in front of a number without actually changing the underlying value, the Excel TEXT function pads numbers with zeros by "physically" inserting leading zeros in cells.
To add leading zeros with a TEXT(value, format_text) formula, you use the same format codes as in custom number formats. However, the result of the TEXT function is always a text string, even if it looks much like a number.
For example, to insert 0 before a value in cell A2, use this formula:
=TEXT(A2, "0#")
To create a zero-prefixed string of a fixed length, say a 5-character string, use this one:
=TEXT(A2, "000000")
Please pay attention that the TEXT function requires enclosing the format codes in quotation marks. And this is how the results will look like in Excel:
A | B | C | |
---|---|---|---|
1 | Original number | Padded number | Formula |
2 | 1 | 01 | =TEXT(B2, "0#") |
3 | 12 | 12 | =TEXT(B3, "0#") |
4 | 1 | 00001 | =TEXT(B4,"00000") |
5 | 12 | 00012 | =TEXT(B5,"00000") |
For more information about Text formulas, please see How to use the TEXT function in Excel.
Bottom line: The Excel TEXT function always returns a text string, not number, and therefore you won't be able to use the results in arithmetic calculations and other formulas, unless you need to compare the output with other text strings.
In the previous examples, you learned how to add zero before a number in Excel. But what if you need to put zero(s) in front of a text string like 0A102? In that case, neither TEXT nor custom format will work because they deal with numeric values only.
If the value to be padded with zero contains letters or other text characters, use one of the following formulas, which offer a universal solution applicable to both numbers and text strings.
The easiest way to put leading zeros before text strings in Excel is using the RIGHT function:
Where:
For example, to make a zero-prefixed 6-character string based on a value in cell A2, use this formula:
=RIGHT("000000"&A2, 6)
What the formula does is add 6 zeros to the value in A2 ("000000"&A2), and then extract the right 6 characters. As the result, it inserts just the right number of zeros to reach the specified total string limit:
In the above example, the maximum number of zeros equals the total string length (6 characters), and therefore all of the resulting strings are 6-character long (fixed length). If applied to a blank cell, the formula would return a string consisting of 6 zeros.
Depending on your business logic, you can supply different numbers of zeros and total characters, for example:
=RIGHT("00"&A2, 6)
As the result, you will get variable-length strings that contain up to 2 leading zeros:
Another way to insert leading zeros before a text string in Excel is using this combination of REPT and LEN functions:
For example, to add leading zeroes to the value in A2 to create a 6-character string, this formula goes as follows:
=REPT(0, 6-LEN(A2))&A2
Knowing that the REPT function repeats a given character a specified number of times, and LEN returns the total length of the string, the formula's logic is easy to understand:
Bottom line: This formula can add leading zeros both to numbers and text strings, but the result is always text, not number.
To prefix all values in a column (numbers or text strings) with a certain number of zeros, use either the CONCATENATE function or the ampersand operator.
For example, to put 0 before a number in cell A2, use one of these formulas:
=CONCATENATE(0,A2)
or
=0&A2
As shown in the screenshot below, the formula adds just one leading zero to all cells in a column regardless of how many characters the original value contains:
In the same manner, you can insert 2 leading zeros (00), 3 zeros (000) or as many zeros as you want before numbers and text strings.
Bottom line: The result of this formula is also a text string even when you are concatenating zeros with numbers.
The method you use to remove leading zeros in Excel depends on how those zeros were added:
The following image shows all three cases to help you choose the right technique:
If leading zeroes are shown in cells with a custom format, then change the cell format back to default General, or apply another number format that does not display preceding zeros.
When prefixed zeros appear in a Text-formatted cell, the easiest way to remove them is select the cell(s), click the exclamation point, and then click Convert to Number:
In case a preceding zero(s) is added with a formula, use another formula to remove it. The zero-removing formula is as simple as:
=VALUE(A2)
Where A2 is the cell from which you want to remove preceding zeros.
This method can also be used to get rid of zeros typed directly in cells (like in the previous example) or imported to Excel from some external source. Overall, if you are dealing with a zero-prefixed string that represents a number, you can use the VALUE function to convert text to number and remove leading zeros along the way.
The following screenshot shows two formulas:
If you don't want to display zero values in your Excel sheet, you have the following two options:
For this, select the cells where you want to hide zeros, click Ctrl+1 to open the Format Cells dialog, select Custom under Category, and type the above format code in the Type box.
The screenshot below shows that cell B2 does contain a zero value, but it is not displayed in the cell:
Finally, good news for users of our Ultimate Suite for Excel - a new tool especially designed for handling zeros is released! Please welcome Add/Remove Leading Zeros.
As usual, we've strived to reduce the number of moves to an absolute minimum :)
To add leading zeros, here's what you do:
Done!
To remove leading zeros, the steps are very much alike:
The add-in can add leading zeros to both numbers and strings:
This is how you can add, remove and hide zeros in Excel. To better understand the techniques described in this tutorial, you are welcome to download the sample workbook. I thank you for reading and hope to see you on our blog next week!
Excel Leading Zeros examples (.xlsx file)
Ultimate Suite 14-day fully-functional version (.zip file)
Table of contents