﻿ Keep leading zeros in number codes

# Keep leading zeros in number codes

Do you ever use things like social security numbers, phone numbers, credit card numbers, product codes, or postal codes in your workbooks? These are all examples of number codes—things that look like numbers, but that aren’t intended to be used in formulas.

When you import or paste numbers codes like these into your workbook, Excel interprets them as numbers and applies a general or number format to them. As a consequence, any leading zeros are removed from the number codes, which is often not what you want. In this article, you’ll learn how to retain leading zeros in number codes.

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

## Why Excel removes leading zeros

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. 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) Socialsecurity 012345678 123456781.2E+07 Phone 0014255550177 142555501771.4E+10 Postalcode 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 databases or 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 format to keep the leading zeros

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. This works for number codes that contain fewer than 16 digits,

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 Postalcode 00123 00000 00123

Procedure

1. Select the cell or range of cells that you want to format.

How to select a cell or a range

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

2. On the Home tab, in the Number group, click the dialog box launcher next to Number.

3. In the Category list, 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 five-digit postal code.

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

### Leading zeros and other programs

If you are using another program that opens your workbook as a data source, you should see how that program handles leading zeros.

For example, suppose you want to use a list of addresses in a workbook as the data source for a Mail Merge operation, and one of the columns is a zip code with a custom format of 00000. In this scenario, Access keeps the leading zeros, but 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 Socialsecurity 012345678 =TEXT(A1,"000-00-0000")012-34-5678 Phone 0012345556789 =TEXT(A1,"00-0-000-000-0000")00-1-234-555-6789 Postalcode 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) Creditcard 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(Withleadingzero) 0123456789012345 0000-0000-0000-00000123-4567-8901-2345 Credit card(Withoutleadingzero) 1234567890123456 0000-0000-0000-00001234-5678-9012-3450

In the case of number codes that are 16 digits or larger, you must use a text format. To do this, you can do one of two things:

#### Use the apostrophe character

For a small set of number codes, 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.

Applies To: Excel 2010

﻿