Chapter 24. Using Custom Number Formats

IN THIS CHAPTER

An overview of custom number formatting

How to create a custom number format

A list of all custom number format codes

Examples of custom number formats

When you enter a number into a cell, you can display that number in a variety of different formats. Excel has quite a few built-in number formats, but you may find that none of them suit your needs.

This chapter describes how to create custom number formats and provides many examples that you can use as-is, or adapt to your needs.

24.1. About Number Formatting

By default, all cells use the General number format. This format is basically "what you type is what you get." But if the cell isn't wide enough to show the entire number, the General format rounds numbers with decimals and uses scientific notation for large numbers. In many cases, the General number format works just fine, but most people prefer to specify a different number format for consistency.

The key thing to remember about number formatting is that it affects only how a value is displayed. The actual number remains intact, and any formulas that use a formatted number use the actual number.

NOTE

An exception to this rule occurs if you specify the Set Precision as Displayed option on the Advanced tab in the Excel Options dialog box. If that option is in effect, formulas use the values that are actually displayed in the cells. In general, using this option is not a good idea because it changes the underlying values in your worksheet.

One more thing to keep in mind: If you use the Find and Replace dialog box (Home Editing Find & Select Find), characters that are displayed as a result of number formatting (for example, a currency symbol) are not searchable by default. To be able to locate information based on formatting, use the Search In Value option in the Find and Replace dialog box.

24.1.1. Automatic number formatting

Excel is smart enough to perform some formatting for you automatically. For example, if you enter 12.3% into a cell, Excel assumes that you want to use a percentage format and applies it automatically. If you use commas to separate thousands (such as 123,456), Excel applies comma formatting for you. And if you precede your value with a currency symbol, Excel formats the cell for currency.

NOTE

You have an option when it comes to entering values into cells formatted as percentages. Access the Excel Options and click the Advanced tab. If the Enable Automatic Percent Entry check box is selected (the default setting), you can simply enter a normal value into a cell that has been formatted to display as a percent (for example, enter 12.5 for 12.5%). If this check box isn't selected, you must enter the value as a decimal (for example, .125 for 12.5%).

Excel automatically applies a built-in number format to a cell based on the following criteria:

  • If a number contains a slash (/), it may be converted to a date format or a fraction format.

  • If a number contains a hyphen (-), it may be converted to a date format.

  • If a number contains a colon (:) or is followed by a space and the letter A or P, it may be converted to a time format.

  • If a number contains the letter E (uppercase or lowercase), it may be converted to scientific notation or exponential format.

To avoid automatic number formatting when you enter a value, preformat the cell with the desired number format or precede your entry with an apostrophe. (The apostrophe makes the entry text, so number formatting is not applied to the cell.)


24.1.2. Formatting numbers by using the Ribbon

The Number group on the Home tab of the Ribbon contains several controls for applying common number formats quickly. The Number Format drop-down control gives you quick access to 11 common number formats. In addition, the Number group contains some buttons. When you click one of these buttons, the selected cells take on the specified number format. Table 24.1 summarizes the formats that these buttons perform in the U.S. English version of Excel.

NOTE

Some of these buttons actually apply predefined styles to the selected cells. Access Excel's styles by using the Style gallery, in the Styles group on the Home tab.

Table 24.1. Number-Formatting Buttons on the Ribbon
Button NameFormatting Applied
Accounting Number FormatAdds a dollar sign to the left, separates thousands with a comma, and displays the value with two digits to the right of the decimal point. This is a drop-down control, so you can select other common currency symbols.
Percent StyleDisplays the value as a percentage, with no decimal places.
Comma StyleSeparates thousands with a comma and displays the value with two digits to the right of the decimal place. It's like the Accounting number format, but without the currency symbol.
Increase DecimalIncreases the number of digits to the right of the decimal point by one.
Decrease DecimalDecreases the number of digits to the right of the decimal point by one.

24.1.3. Using shortcut keys to format numbers

Another way to apply number formatting is to use shortcut keys. Table 24.2 summarizes the shortcut key combinations that you can use to apply common number formatting to the selected cells or range. Notice that these are the shifted versions of the number keys along the top of a typical keyboard.

Table 24.2. Number-Formatting Keyboard Shortcuts
Key CombinationFormatting Applied
Ctrl+Shift+~General number format (that is, unformatted values).
Ctrl+Shift+!Two decimal places, thousands separator, and a hyphen for negative values.
Ctrl+Shift+@Time format with the hour, minute, and AM or PM.
Ctrl+Shift+#Date format with the day, month, and year.
Ctrl+Shift+$Currency format with two decimal places. (Negative numbers appear in parentheses.)
Ctrl+Shift+%Percentage format with no decimal places.
Ctrl+Shift+^Scientific notation number format with two decimal places.

24.1.4. Using the Format Cells dialog box to format numbers

For maximum control of number formatting, use the Number tab in the Format Cells dialog box. You can access this dialog box in any of several ways:

  • Click the dialog box launcher at the bottom right of the Home Number group.

  • Choose Home Number Number Format More Number Formats.

  • Press Ctrl+1.

The Number tab in the Format Cells dialog box contains 12 categories of number formats from which to choose. When you select a category from the list box, the right side of the dialog box changes to display appropriate options.

Here are the number-format categories, along with some general comments:

  • General: The default format; it displays numbers as integers, decimals, or in scientific notation if the value is too wide to fit into the cell.

  • Number: Specify the number of decimal places, whether to use your system thousands separator (for example, a comma) to separate thousands, and how to display negative numbers.

  • Currency: Specify the number of decimal places, choose a currency symbol, and display negative numbers. This format always uses the system thousands separator symbol (for example, a comma) to separate thousands.

  • Accounting: Differs from the Currency format in that the currency symbols always line up vertically, regardless of the number of digits displayed in the value.

  • Date: Choose from a variety of date formats and select the locale for your date formats.

  • Time: Choose from a number of time formats and select the locale for your time formats.

  • Percentage: Choose the number of decimal places; always displays a percent sign.

  • Fraction: Choose from among nine fraction formats.

  • Scientific: Displays numbers in exponential notation (with an E): 2.00E+05 = 200,000. You can choose the number of decimal places to display to the left of E.

  • Text: When applied to a value, causes Excel to treat the value as text (even if it looks like a value). This feature is useful for such items as numerical part numbers and credit card numbers.

  • Special: Contains additional number formats. The list varies, depending on the Locale you choose. For the English (United States) locale, the formatting options are Zip Code, Zip Code +4, Phone Number, and Social Security Number.

  • Custom: Define custom number formats not included in any of the other categories.

NOTE

If the cell displays a series of hash marks after you apply a number format (such as #########), it usually means that the column isn't wide enough to display the value by using the number format that you selected. Either make the column wider (by dragging the right border of the column header) or change the number format. A series of hash marks also can mean that the cell contains an invalid date or time.

24.2. Creating a Custom Number Format

When you create a custom number format, it can be used to format any cells in the workbook. You can create as many custom number formats as you need.

Figure 24.1 shows the Custom category in the Number tab of the Format Cells dialog box. Here, you can create number formats not included in any of the other categories. Excel gives you a great deal of flexibility in creating custom number formats.

Figure 24.1. The Custom category of the Number tab in the Format Cells dialog box.

Custom number formats are stored with the workbook in which they are defined. To make the custom format available in a different workbook, you can just copy a cell that uses the custom format to the other workbook.


You construct a number format by specifying a series of codes as a number format string. You enter this code sequence in the Type field after you select the Custom category on the Number tab of the Format Cells dialog box. Here's an example of a simple number format code:

0.000

This code consists of placeholders and a decimal point; it tells Excel to display the value with three digits to the right of the decimal place. Here's another example:

00000

This custom number format has five placeholders and displays the value with five digits (no decimal point). This format is good to use when the cell holds a five-digit zip code. (In fact, this is the code actually used by the Zip Code format in the Special category.) When you format the cell with this number format and then enter a Zip Code, such as 06604 (Bridgeport, CT), the value is displayed with the leading zero. If you enter this number into a cell with the General number format, it displays 6604 (no leading zero).

Scroll through the list of number formats in the Custom category in the Format Cells dialog box to see many more examples. In many cases, you can use one of these codes as a starting point, and you'll need to customize it only slightly.

NOTE

The companion CD-ROM contains a workbook with many custom number format examples (see Figure 24.2). The file is named number formats.xlsx.

Changing the Default Number Format for a Workbook

As I mention earlier, the default number format is General. If you prefer a different default number format, you have two choices: Preformat the cells with the number format of your choice, or change the number format for the Normal style.

You can preformat specific cells, entire rows or columns, or even the entire worksheet.

Rather than preformat an entire worksheet, however, a better solution is to change the number format for the Normal style. Unless you specify otherwise, all cells use the Normal style. Therefore, by changing the number format for the Normal style you are essentially creating a new default number format for the workbook.

Change the Normal style by displaying the Style gallery. Right-click the Normal style icon (in the Home Styles group) and choose Modify to display the Style dialog box. In the Style dialog box, click the Format button and then choose the new number format that you want to use for the Normal style.


Figure 24.2. Examples of custom number formatting.

24.2.1. Parts of a number format string

A custom format string can have up to four sections, which enables you to specify different format codes for positive numbers, negative numbers, zero values, and text. You do so by separating the codes with a semicolon. The codes are arranged in the following order:

Positive format; Negative format; Zero format; Text format

If you don't use all four sections of a format string, Excel interprets the format string as follows:

  • If you use only one section: The format string applies to all types of entries.

  • If you use two sections: The first section applies to positive values and zeros, and the second section applies to negative values.

  • If you use three sections: The first section applies to positive values, the second section applies to negative values, and the third section applies to zeros.

  • If you use all four sections: The last section applies to text stored in the cell.

The following is an example of a custom number format that specifies a different format for each of these types:

[Green]General;[Red]General;[Black]General;[Blue]General

This custom number format example takes advantage of the fact that colors have special codes. A cell formatted with this custom number format displays its contents in a different color, depending on the value. When a cell is formatted with this custom number format, a positive number is green, a negative number is red, a zero is black, and text is blue.

NOTE

If you want to apply cell formatting automatically (such as text or background color) based on the cell's contents, a much better solution is to use the Excel Conditional Formatting feature. Chapter 20 covers conditional formatting.

24.2.2. Custom number format codes

Table 24.3 lists the formatting codes available for custom formats, along with brief descriptions. I use most of these codes in examples later in this chapter.

Table 24.3. Codes Used To Create Custom Number Formats
CodeComments
GeneralDisplays the number in General format.
#Digit placeholder. Displays only significant digits, and does not display insignificant zeros.
0 (zero)Digit placeholder. Displays insignificant zeros if a number has fewer digits than there are zeros in the format.
?Digit placeholder. Adds spaces for insignificant zeros on either side of the decimal point so that decimal points align when formatted with a fixed-width font. You can also use ? for fractions that have varying numbers of digits.
.Decimal point.
%Percentage.
,Thousands separator.
E- E+ e- e+Scientific notation.
$ - + / ( ) : spaceDisplays this character.
Displays the next character in the format.
*Repeats the next character, to fill the column width.
_ (underscore)Leaves a space equal to the width of the next character.
"text"Displays the text inside the double quotation marks.
@Text placeholder.
[color]Displays the characters in the color specified. Can be any of the following text strings (not case sensitive): Black, Blue, Cyan, Green, Magenta, Red, White, or Yellow.
[Color n]Displays the corresponding color in the color palette, where n is a number from 0 to 56.
[condition value]Set your own criterion for each section of a number format.

Table 24.4 lists the codes used to create custom formats for dates and times.

Table 24.4. Codes Used In Creating Custom Formats For Dates And Times
CodeComments
mDisplays the month as a number without leading zeros (1–12).
mmDisplays the month as a number with leading zeros (01–12).
mmmDisplays the month as an abbreviation (Jan–Dec).
mmmmDisplays the month as a full name (January–December).
mmmmmDisplays the first letter of the month (J–D).
dDisplays the day as a number without leading zeros (1–31).
ddDisplays the day as a number with leading zeros (01–31).
dddDisplays the day as an abbreviation (Sun–Sat).
ddddDisplays the day as a full name (Sunday–Saturday).
yy or yyyyDisplays the year as a two-digit number (00–99) or as a four-digit number (1900–9999).
h or hhDisplays the hour as a number without leading zeros (0–23) or as a number with leading zeros (00–23).
m or mmDisplays the minute as a number without leading zeros (0–59) or as a number with leading zeros (00–59).
s or ssDisplays the second as a number without leading zeros (0–59) or as a number with leading zeros (00–59).
[ ]Displays hours greater than 24 or minutes or seconds greater than 60.
AM/PMDisplays the hour using a 12-hour clock; if no AM/PM indicator is used, the hour uses a 24.hour clock.

Where Did Those Number Formats Come From?

Excel may create custom number formats without you realizing it. When you use the Increase Decimal or Decrease Decimal button on the Home Number group of the Ribbon (or on the Mini toolbar), Excel creates new custom number formats, which appear on the Number tab in the Format Cells dialog box. For example, if you click the Increase Decimal button five times, the following custom number formats are created:

0.0
0.000
0.0000
0.000000

A format string for two decimal places is not created because that format string is built-in.


24.3. Custom Number Format Examples

The remainder of this chapter consists of useful examples of custom number formats. You can use most of these format codes as-is. Others may require slight modification to meet your needs.

24.3.1. Scaling values

You can use a custom number format to scale a number. For example, if you work with very large numbers, you may want to display the numbers in thousands (that is, display 1,200,000 as 1,200). The actual number, of course, will be used in calculations that involve that cell. The formatting affects only how it is displayed.

24.3.1.1. Displaying values in thousands

The following format string displays values without the last three digits to the left of the decimal place and no decimal places. In other words, the value appears as if it's divided by 1,000 and rounded to no decimal places.

#,###,

A variation of this format string follows. A value with this number format appears as if it's divided by 1,000 and rounded to two decimal places.

#,###.00,

Table 24.5 shows examples of these number formats:

Table 24.5. Examples Of Displaying Values In Thousands
ValueNumber FormatDisplay
123456#,###,123
1234565#,###,1,235
−323434#,###,−323
123123.123#,###,123
499#,###,(blank)
500#,###,1
123456#,###.00,123.46
1234565#,###.00,1,234.57
−323434#,###.00,−323.43
123123.123#,###.00,123.12
499#,###.00,.50
500#,###.00,.50

24.3.2. Displaying values in hundreds

The following format string displays values in hundreds, with two decimal places. A value with this number format appears as if it's divided by 100 and rounded to two decimal places.

0"."00

Table 24.6 shows examples of these number formats:

Table 24.6. Examples Of Displaying Values In Hundreds
ValueNumber FormatDisplay
5460"."005.46
1000"."001.00
98900"."0098.90
5000"."005.00
−5000"."00−5.00
00"."000.00

24.3.3. Displaying values in millions

The following format string displays values in millions with no decimal places. A value with this number appears as if it's divided by 1,000,000 and rounded to no decimal places.

#,###,,

A variation of this format string follows. A value with this number appears as if it's divided by 1,000,000 and rounded to two decimal places.

#,###.00,,

Another variation follows. This adds the letter M to the end of the value.

#,###,,M

The following format string is a bit more complex. It adds the letter M to the end of the value — and also displays negative values in parentheses as well as displaying zeros.

#,###.0,,"M"_);(#,###.0,,"M)";0.0"M"_)

Table 24.7 shows examples of these format strings.

Table 24.7. Examples of Displaying Values in Millions
ValueNumber FormatDisplay
123456789#,###,,123
1.23457E+11#,###,,123,457
1000000#,###,,1
5000000#,###,,5
−5000000#,###,,−5
0#,###,,(blank)
123456789#,###.00,,123.46
1.23457E+11#,###.00,,123,457.00
1000000#,###.00,,1.00
5000000#,###.00,,5.00
−5000000#,###.00,,−5.00
0#,###.00,,.00
123456789#,###,,"M"123M
1.23457E+11#,###,,"M"123,457M
1000000#,###,,"M"1M
5000000#,###,,"M"5M
−5000000#,###,,"M"−5M
0#,###,,"M"M
123456789#,###.0,,"M"_);(#,###.0,,"M)";0.0"M"_)123.5M
1.23457E+11#,###.0,,"M"_);(#,###.0,,"M)";0.0"M"_)123,456.8M
1000000#,###.0,,"M"_);(#,###.0,,"M)";0.0"M"_)1.0M
5000000#,###.0,,"M"_);(#,###.0,,"M)";0.0"M"_)5.0M
−5000000#,###.0,,"M"_);(#,###.0,,"M)";0.0"M"_)(5.0M)
0#,###.0,,"M"_);(#,###.0,,"M)";0.0"M"_)0.0M

24.3.4. Adding zeros to a value

The following format string displays a value with three additional zeros and no decimal places. A value with this number format appears as if it's rounded to no decimal places and then multiplied by 1,000.

#",000"

Examples of this format string, plus a variation that adds six zeros, are shown in Table 24.8.

Table 24.8. Examples of Displaying a Value with Extra Zeros
ValueNumber FormatDisplay
1#",000"1,000
1.5#",000"2,000
43#",000"43,000
−54#",000"−54,000
5.5#",000"6,000
0.5#",000,000"1,000,000
0#",000,000",000,000
1#",000,000"1,000,000
1.5#",000,000"2,000,000
43#",000,000"43,000,000
−54#",000,000"−54,000,000
5.5#",000,000"6,000,000
0.5#",000,000"1,000,000

24.3.5. Displaying leading zeros

To display leading zeros, create a custom number format that uses the 0 character. For example, if you want all numbers to display with ten digits, use the number format string that follows. Values with fewer than ten digits will display with leading zeros.

0000000000

You also can force all numbers to display with a fixed number of leading zeros. The format string that follows, for example, appends three zeros to the beginning of each number:

"000"#

In the following example, the format string uses the repeat character code (an asterisk) to apply enough leading zeros to fill the entire width of the cell:

*00

24.3.6. Displaying fractions

Excel supports quite a few built-in fraction number formats (select the Fraction category on the Number tab in the Format Cells dialog box). For example, to display the value .125 as a fraction with 8 as the denominator, select As Eighths (4/8) from the Type list (see Figure 24.3).

Figure 24.3. Selecting a number format to display a value as a fraction.

Testing Custom Number Formats

When you create a custom number format, don't overlook the Sample box on the Number tab in the Format Cells dialog box. This box displays the value in the active cell using the format string in the Type box.

It's a good idea to test your custom number formats by using the following data: a positive value, a negative value, a zero value, and text. Often, creating a custom number format takes several attempts. Each time you edit a format string, it is added to the list. When you finally get the correct format string, access the Format Cells dialog box one more time and delete your previous attempts.


You can use a custom format string to create other fractional formats. For example, the following format string displays a value in 50ths:

# ??/50

To display the fraction reduced to its lowest terms, use a question mark after the slash symbol. For example, the value 0.125 can be expressed as 2/16, and 2/16 can be reduced to 1/8. Here's an example of a number format that displays the value as a fraction reduced to its simplest terms:

# ?/?

If you omit the leading hash symbol, the value is displayed without a leading value. For example, the value 2.5 would display as 5/2 using this number format code:

?/?

The following format string displays a value in terms of fractional dollars. For example, the value 154.87 is displayed as 154 and 87/100 Dollars.

0 "and "??/100 "Dollars"

The following example displays the value in sixteenths, with a quotation mark appended to the right. This format string is useful when you deal with fractions of inches (for example, 2/16″).

# ??/16"

24.3.7. Displaying a negative sign on the right

The following format string displays negative values with the negative sign to the right of the number. Positive values have an additional space on the right, so both positive and negative numbers align properly on the right.

0.00_-;0.00-

To make the negative numbers more prominent, you can add a color code to the negative part of the number format string:

0.00_-;[Red]0.00-

24.3.8. Formatting dates and times

When you enter a date into a cell, Excel formats the date using the system short date format. You can change this format by using the Windows Control Panel (Regional and Language Options).

Excel provides many useful, built-in dates and time formats. Table 24.9 shows some other date and time formats that you may find useful. The first column of the table shows the date/time serial number.

Table 24.9. Useful Built-In Date and Time Formats
ValueNumber FormatDisplay
40360mmmm d, yyyy (dddd)July 1, 2007 (Thursday)
40360"It's" dddd!It's Thursday!
40360dddd, mm/dd/yyyyThursday, 07/01/2010
40360"Month: "mmmMonth: July
40360General (m/d/yyyy)40360 (7/1/2010)
0.345h "Hours"8 Hours
0.345h:mm o'clock8:16 o'clock
0.345h:mm a/p"m"8:16 am
0.78h:mm a/p".m."6:43 p.m.

NOTE

See Chapter 13 for more information about the Excel date and time serial number system.

24.3.9. Displaying text with numbers

The ability to display text with a value is one of the most useful benefits of using a custom number format. To add text, just create the number format string as usual (or use a built-in number format as a starting point) and put the text within quotation marks. The following number format string, for example, displays a value with the text (US Dollars) added to the end:

#,##0.00 "(US Dollars)"

Using the TEXT Function to Format Numbers

The TEXT function accepts a number format string as its second argument. For example, the following formula displays the contents of cell A1 using a custom number format that displays a fraction:

=TEXT(A1,"# ??/50")

However, not all formatting codes work when used in this manner. For example, colors and repeating characters are ignored. The following formula does not display the contents of cell A1 in red:

=TEXT(A1,"[Red]General")


Here's another example that displays text before the number:

"Average: "0.00

If you use the preceding number format, you'll find that the negative sign appears before the text for negative values. To display number signs properly, use this variation:

"Average: "0.00;"Average: "−0.00

The following format string displays a value with the words Dollars and Cents. For example, the number 123.45 displays as 123 Dollars and .45 Cents.

0 "Dollars and" .00 "Cents"

24.3.10. Suppressing certain types of entries

You can use number formatting to hide certain types of entries. For example, the following format string displays text but not values:

;;

This format string displays values but not text or zeros:

0.0;−0.0;;

This format string displays everything except zeros:

0.0;−0.0;;@

You can use the following format string to completely hide the contents of a cell:

;;;

Note that when the cell is activated, however, the cell's contents are visible on the Formula bar.

24.3.11. Filling a cell with a repeating character

The asterisk (*) symbol specifies a repeating character in a number format string. The repeating character completely fills the cell and adjusts if the column width changes. The following format string, for example, displays the contents of a cell padded on the right with dashes:

General*-;−General*-;General*-;General*-

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset