How to handle leading zeros and precision in number codes

You may sometimes use social security, phone, and postal code numbers in your Office Excel data. What do these numbers have in common? Each of them is a number code that you don't use to count, but you may want to store them as numbers anyway. Unfortunately, Excel removes leading zeros that you may want to stay right where they are. Here's how to make Excel handle leading zeros in number codes the way that you want.

If you are using credit card numbers, or other number codes that contain 16 digits or more, you must use a text format because Excel has a maximum of 15 digits of precision and will round any numbers that follow the 15th digit down to zero.

In this article


The number format removes leading zeros by default

Number codes get into your Excel workbook in many ways. You might simply type them in, or copy and paste them from another program. Or, you might open a text file, or import data from a data source, such as an Access database. In many cases, Excel converts these number codes to a general or a number format. The default behavior of those formats is to remove any leading zeros and, depending on the length of the number, use scientific notation. You see, Excel treats a number code as just another number, but you know that it's a number code, and that the leading zeros need to stay put.

Number
code
Fictitious example Default behaviors
(varies with cell width)
Social
security
012345678 12345678
1.2E+07
Phone 0014255550177 14255550177
1.4E+10
Postal
code
00123 123

You might not even see this happen if you are dealing with a long list of number codes. So it's important to be aware of the issue, especially when the data is used in other programs. Although you can convert the number format to a text format, you may not want to convert numbers to text, especially if you have a large list of long numbers, because this can increase the size of your workbook.

Use a custom or special format to keep the leading zeros

For number codes that contain fewer than 16 digits, if you want to resolve the issue just within the workbook because it's not used by other programs as a data source, you can use a custom or a special format to keep the leading zeros.

In addition, you can separate some of the digits in your number codes with dashes by adding these dashes to the custom format. For example, to make a phone number more readable, you can add a dash between the international code, the country/region code, the area code, the prefix, and the last few numbers.

Number
code
Fictitious example Number format and
new behavior
Social
security
012345678 000-00-0000 
012-34-5678 
Phone 0012345556789 00-0-000-000-0000 
00-1-234-555-6789 
Postal
code
00123 00000 
00123 

Procedure    

  1. Select the cell or range (range: Two or more cells on a sheet. The cells in a range can be adjacent or nonadjacent.) of cells that you want to format.

How to select a cell or a range

To select Do this
A single cell Click the cell, or press the arrow keys to move to the cell.
A range of cells

Click the first cell in the range, and then drag to the last cell, or hold down SHIFT while you press the arrow keys to extend the selection.

You can also select the first cell in the range, and then press F8 to extend the selection by using the arrow keys. To stop extending the selection, press F8 again.

A large range of cells Click the first cell in the range, and then hold down SHIFT while you click the last cell in the range. You can scroll to make the last cell visible.
All cells on a worksheet

Click the Select All button.

Select All button

To select the entire worksheet, you can also press CTRL+A.

 Note   If the worksheet contains data, CTRL+A selects the current region. Pressing CTRL+A a second time selects the entire worksheet.

Nonadjacent cells or cell ranges

Select the first cell or range of cells, and then hold down CTRL while you select the other cells or ranges.

You can also select the first cell or range of cells, and then press SHIFT+F8 to add another nonadjacent cell or range to the selection. To stop adding cells or ranges to the selection, press SHIFT+F8 again.

 Note   You cannot cancel the selection of a cell or range of cells in a nonadjacent selection without canceling the entire selection.

An entire row or column

Click the row or column heading.

Worksheet showing row heading and column heading

Callout 1 Row heading
Callout 2 Column heading

You can also select cells in a row or column by selecting the first cell and then pressing CTRL+SHIFT+ARROW key (RIGHT ARROW or LEFT ARROW for rows, UP ARROW or DOWN ARROW for columns).

 Note   If the row or column contains data, CTRL+SHIFT+ARROW key selects the row or column to the last used cell. Pressing CTRL+SHIFT+ARROW key a second time selects the entire row or column.

Adjacent rows or columns Drag across the row or column headings. Or select the first row or column; then hold down SHIFT while you select the last row or column.
Nonadjacent rows or columns Click the column or row heading of the first row or column in your selection; then hold down CTRL while you click the column or row headings of other rows or columns that you want to add to the selection.
The first or last cell in a row or column Select a cell in the row or column, and then press CTRL+ARROW key (RIGHT ARROW or LEFT ARROW for rows, UP ARROW or DOWN ARROW for columns).
The first or last cell on a worksheet or in a Microsoft Office Excel table

Press CTRL+HOME to select the first cell on the worksheet or in an Excel list.

Press CTRL+END to select the last cell on the worksheet or in an Excel list that contains data or formatting.

Cells to the last used cell on the worksheet (lower-right corner) Select the first cell, and then press CTRL+SHIFT+END to extend the selection of cells to the last used cell on the worksheet (lower-right corner).
Cells to the beginning of the worksheet Select the first cell, and then press CTRL+SHIFT+HOME to extend the selection of cells to the beginning of the worksheet.
More or fewer cells than the active selection Hold down SHIFT while you click the last cell that you want to include in the new selection. The rectangular range between the active cell and the cell that you click becomes the new selection.

 Tip   To cancel a selection of cells, click any cell on the worksheet.


  1. On the Home tab, click the Dialog Box Launcher Button image next to Number.

Excel Ribbon Image

  1. In the Category box, click Custom and then, in the Type box, type the number format, such as 000-00-0000 for a social security number code, or 00000 for a postal code.

 Tip   You can also click Special, and then select Zip Code, Zip Code + 4, Phone number, or Social Security Number.

Find more information about custom codes in the See Also section.

Use a formula or calculated column

If you are using another program that opens the workbook as a data source, you should confirm to see how that program handles leading zeros. For example, you have a list of addresses in a workbook that you want to use as a data source to a Mail Merge operation, and one of the columns is a zip code with a custom format of 00000. Office Access keeps the leading zeros, but Office Word removes the leading zeros. In the case of Word, you can specify a calculated column as the postal code field in the Mail Merge operation to ensure that the leading zeros are not removed.

Number
code
Fictitious example
(In cell A1)
TEXT function and
new behavior
Social
security
012345678 =TEXT(A1,"000-00-0000")
012-34-5678
Phone 0012345556789 =TEXT(A1,"00-0-000-000-0000")
00-1-234-555-6789
Postal
code
00123 =TEXT(A1,"00000")
00123

Credit card number codes are rounded down

To further complicate the matter, Excel has a maximum precision of 15 significant digits, which means that for any number containing 16 or more digits, such as a credit card number, the last digit is rounded down to zero.

Number
code
Fictitious example Default behaviors
(varies with cell width)
Credit
card
0123456789012345 1.23457E+14

Even if you use a custom number format, the number is still rounded down to zero when there is no leading zero.

Number
code
Fictitious example Number format and
new behavior
Credit
card
(With
leading
zero)
0123456789012345 0000-0000-0000-0000
0123-4567-8901-2345
Credit
card
(Without
leading
zero)
1234567890123456 0000-0000-0000-0000
1234-5678-9012-3450

In the case of number codes that are 16 digits or larger, you must use a text format.

Top of Page Top of Page

Convert the number code to a text format

To convert a number code, such as a credit card number to text format, you can do the following:

Use the apostrophe character

For a small set of numbers, you can type an apostrophe character (') in front of the number code and then add back the leading zeros.

Convert the number to text when you import text data

In Step 3 of the Text Import Wizard (On the Data tab, in the Get External Data group, click From Text), you can select the column of data that contains the credit card number, and then explicitly choose a Text column data format.

Top of Page Top of Page

 
 
Applies to:
Excel 2007