- 1 Introduction
- 2 Structure
- 3 Character Codes
- 4 Conditional Number Formats
- 5 Using Colours
- 6 Examples
- 6.1 Display a signature strip
- 6.2 Prefix a number with a single character
- 6.3 Suffix a number with a text string
- 6.4 Pad a number with n leading zero’s
- 6.5 Align Positive and Negative numbers
- 6.6 Understanding the thousand separator
- 6.7 Scale down a number by a factor of 1,000
- 6.8 Format percentages
- 6.9 #Display cumulative elapsed time (hours/minutes/seconds)
- 6.10 Scale a number by a factor of 10
- 6.11 A quick guide to fractions
- 6.12 Colour coding cells according to value
- 6.13 Use symbols within number formats
- 7 Further Reading
Number formatting in Excel is used to change the way a value appears in a cell or range of cells. Generally speaking number formatting does not actually alter the value, it only changes the way the we see the it.
This isn’t true in all cases, numbers formatted as text are no longer considered as number types and thus it can be suggested that number formatting can change the value of the cell.
A more accurate method of determining a cells true value is to focus on the value as it appears in the formula bar.
When one formats a cell one typically uses the Format Cells dialog (Control+1).
There are a variety of predefined formats to choose from in the Category list. The underlying code to any one of these formats can be seen by selecting the format from the list and then by immediately proceeding to Custom format. One can define and alter number formats using the Custom category.
Number formatting can be used in some very creative ways. There are many occasions where the unaware user constructs complex formulae to display results, or uses conditional formatting, where an otherwise relatively short number format code would do the same.
Number formats are separated into four components and each component is separated by a semi-colon (;). These components are, in order;
- positive numbers (+ve)
- negative numbers (-ve)
- text values
The semi-colon not only acts as a separator for these specific components, but also acts as a separator for conditional components (but we will get to that later).
One needn’t complete each component of a number format. Where one or more of these components are omitted, Excel will make the following assumptions:
- If only one component is used (i.e. there are no semi-colons present in the code), then all numbers will be formatted accordingly, regardless of whether or not the number is +ve, -ve or zero.
- If two components are used then the 1st component will be used for both +ve numbers and zero value. The 2nd component is used for -ve numbers.
- If three components are used then then 1st component will be used for +ve numbers. The 2nd will be used for -ve numbers. The 3rd will be used for zero’s. Text values will appear as typed.
Thus, any cell formatted with the following code;
will appear blank regardless of whether or not the cell contains a number (+ve, -ve or 0) or text. Every number format component is complete in this code, yet blank, and hence so shall the appearance of the cell be.
Note: Cell error values (e.g. #N/A) are not affected by number formats.
This section describes what the various characters in a number format code means* These characters can be categorised as Text, Number, Date and Time.
Literal Characters; i.e. these characters will appear in the cell according to where in the number format code they are entered.
|“”||Displays the literal text between the speech marks.|
|\||Displays the proceeding [single] character as a literal character.|
|@||Displays the text entered in the cell.|
|_||(underscore) Displays a single character [blank] space according to the width of the proceeding character (i.e. the proceeding character will not be displayed).|
|*||Displays the proceeding [single] character as many times is required to fill the width of the cell. This proceeding character will always be taken as a literal, regardless of what character is actually used.|
|#||Displays only significant digits (i.e. the numbers entered into the cell). It does not display insignificant zero’s.|
|0||(zero) Displays both significant digits (i.e. those entered into the cell), but also displays insignificant zero’s if a number has fewer digits than there are zero’s in the number format code.|
|.||Decimal separator. The number of proceeding zero’s will determine how many decimal places are displayed.|
|,||Thousands separator if it proceeds #, or scales a number down by a factor of 1000 if it proceeds 0 (zero).|
|?||Digit placeholder; Displays [blank] spaces for insignificant zero’s. Also used for fractions with varying number of digits.|
|%||Scales a number up by a factor of 100 and suffixes the number with the % symbol.|
|E+ or E-||Displays a number in normalized scientific ‘E’ notation. “E-” places a minus sign by -ve exponents. “E+” places a minus sign by -ve exponents and a plus sign by +ve exponents.|
|/||Fraction character. See fraction examples later.|
Note: The decimal and thousand separator character is determined either by the regional settings, or the chosen characters defined in Excel’s advanced options if one chooses to override the default separators. Be aware therefore that these separators may in fact not be the same as described in this article.
|d||Displays the day of the month as a number 1-31 (or how many days there are in the given month) and does not display insignificant zero’s.|
|dd||Displays the day of the month as a number 01-31 (or how many days there are in the given month) and always displays it as a 2-digit number.|
|ddd||Displays the day of the week as Sun-Sat (3 character day)|
|dddd||Displays the day of the week as Sunday-Saturday (complete text)|
|m||Displays the month as a nqmber 1-12 and does not display insignificant zero’s|
|mm||Displays the month as a number 01-12 and always displays it as a 2-digit number.|
|mmm||Disphays the month as Jan-Dec (3 character month).|
|mmmm||Displays the month as January-December (complete text).|
|mmmmm||Displays the month as J-D (only the first character).|
|yy||Displays the last two digits of the year.|
|yyyy||Displays all four digits of the year.|
|h||Displays the hour as a number 0-23 and does not display insignificant zero’s.|
|hh||Displays the hour as a number 00-23 and always displays it as a 2-digit number.|
|[h] or [hh]||Displays the cumulative hours elapsed. The time value could for instance exceed 24 hours (1 day).|
|m||Displays the minutes as a number 0-59 and does not display insignificant zero’s. Note that this character only represents minutes if used within a complete time number format code.|
|mm||Displays the minutes as a number 00-59 and always displays it as a 2-digit number. Note that this character only represents minutes if used within a complete time number format code.|
|[m] or [mm]||Displays the cumulative minutes elapsed. The time value could for instance exceed 60 minutes.|
|s||Displays the seconds as a number 0-59 and does not display insignificant zero’s.|
|ss||Displays the seconds as a number 00-59 and always displays it as a 2-digit number.|
|[s] or [ss]||Displays the cumulative seconds elapsed. The time value could for instance exceed 60 seconds.|
|am/pm||Displays hours based on the 12-hour clock and indicates morning/afternoon with AM/PM. Note, this is case-sensitive.|
Conditional Number Formats
A typical number format already caters for four different conditions. In any single number format code, we can specify a format for +ve numbers, -ve numbers, zero values and text.
However, one can create even more format conditions. Excel provides an ability to format a number according to its’ value.
One does not have to stipulate the exact value for a format; rather one can choose from a whole range of comparison operators.
|<>||Does not equal|
|<=||Less than or equal to|
|>=||More than or equal to|
Whilst the semi-colon (;) is used to delimit the standard four components in a number format, the semi-colon is also used to delimit conditions.
Conditions must be enclosed in square brackets, e.g:
[>=1000000]0,, \m;[>=1000]0, \k;0
In the exhibit above, the semi-colon no longer delimits the ordinary components (+ve, -ve, zero, text). The conditional statements now rules the components. In other words, values greater than or equal to 1 million appear scaled to the nearest one million. If the given figure is not greater than or equal to 1 million, but is greater than or equal to 1000, then the figure appears scaled to the nearest one thousand. If neither condition is met then the figure is to be displayed rounded to the nearest whole number.
Another great feature of number formats is to change the font colour of the cell. I use the phrase “change the font colour” loosely, because again cell a values only appear different, the font colour chosen on the ribbon/toolbar will remain unchanged.
Again square brackets [ ] play a role. To format a cell with a colour, one either passes the colour name in square brackets, or one passes the word ‘color’ and the color index in square brackets.
A typical usage is to format -ve figures in red font. For example:
which is the same as:
When formatting colour, one has a choice of 8 standard colours by name, or a choice of 56 colours by colour index:
Display a signature strip
|@*_||Signature||Signature____________||This text format displays an underscore as many times is needed to extend to the end of the cell, creating a solid line.|
|I can’t think of many opportunities to use the asterisk character, so this is my feeble attempt at a demonstration. In this exhibit, the solid line for ones signature will always extend the full width of the cell.|
Prefix a number with a single character
|\Q0||1||Q1||All of these numbers are prefixed with a Q, yet these remain valid numbers.|
|There may be instances where one has a requirement to prefix or suffix a number with a single character. The benefit of this is that one may yet use the number in subsequent formulae, since this still yields a valid number and not text. This display is achieved by using the \ character to treat the ‘Q’ as a literal value.|
Suffix a number with a text string
|0 “days”+||7||7 Days+||All of these numbers are suffixed with ‘Days +’.’Days’ is encapsulated in speech marks to display the literal term. It is not necessary to encapsulate the space and ‘+’ in speech marks as both are literal characters.|
|This exhibit demonstrates how to include a complete text string using speech marks. The space and plus symbol do not need to be held within the speech marks as they are treated as a literal characters regardless. Again, this still returns a valid number and it can be treated as such in formulae.|
Pad a number with n leading zero’s
|Any numbers with fewer than 7 digits will be passed with leading zero’s such that the number displayed will always show at least 7 digits.|
Align Positive and Negative numbers
|0.00_);(0.00)||-1234.56||(1234.56)||Numbers are displayed rounded to the nearest 2 decimals. Negative numbers are surrounded by parenthesis ().|
|1234.56||1234.56||As negative numbers are surrounded by parenthesis, positive numbers accommodate a blank space the same width as closing parenthesis such that the numbers align (right-to-left). This is achieved using the underscore character.|
|This is particularly relevant when enclosing numbers in parenthesis to represent negative values. In doing so the +ve and -ve figures do not align (left-to-right). One uses underscore character to create a blank space and the end of the +ve number with the same width as closing parenthesis.|
Understanding the thousand separator
|#,##0.0||12345.23||12,345.2||All figures displayed include a thousand separator and appear rounded to one decimal place.|
|It is important to note that the last whole number placeholder is a 0 and not #. Take the figure 0.24. Given the current format this would be displayed as “0.2”. However, had the format been #,###.0 (i.e. a # symbol used instead), Excel would only display “.2″. The zero prefix would not be displayed because it is insignificant.
Even if you make the mistake of using a # symbol, Excel will always make this correction regardless.
Scale down a number by a factor of 1,000
|0.0,\k||999||1.0 k||Figures appear rounded to the nearest multiple of 1000, but with one decimal place. It is good practice to suffix such numbers with ‘k’.|
|A single comma character is used to scale and round the figure by 1000.|
|0.0,,\m||999000||1.0 m||Figures appear rounded to the nearest multiple of 1000000, but with one decimal place. It is good practice to suffix such numbers with ‘m. Note how two scale characters (,) are used to achieve a scale of 1 million.|
|Two commas are used to scale and round the figure by 1000000.
Physically dividing a number by 1000 (or 10000) in order to report a scaled down figure is bad practise. This method should be chosen instead. Not only is it unnecessary but it also causes numeric change. Using this number format method does not change the value of the figure, only its’ appearance! The accuracy of figures should not be compromised for the sake of reporting!
|0.0 %_);[Red](0.0 %)||0.23||23.0 %||Figures appear scaled up by 100. Negative figures appear in the red and encapsulated in parenthesis. A % symbol proceeds figures.|
|Some have assumed that because the format cells dialog does not present different formats for negative percentages that it cannot be done. This demonstrates that percentages are merely numbers and thus follow the same rules.|
#Display cumulative elapsed time (hours/minutes/seconds)
|dd/mm/yy h:mm||41113.60625||23/07/12 14:33|
|The typical hour symbol (i.e. h) displays the modulo 24 result. Excel stores time as fractions such that 1 hour is 1/24, 1 minute is 1/24/60 and 1 second is 1/24/60/60. In this exhibit, the difference between 24-Jul-2012 18h21 and 23-Jul-2012 14h33 is 1 day 3 hours and 48 minutes. In Excel this value will be 1.158333333.
Since we are dealing with hours it would be easier to describe the calculation by multiplying 1.58333333 by 24 giving 27.79992 hours. 27.79992 modulo 24 returns 3.79992, which when divided by 24 yields 0.158333333. Note, the integer component (# of days) has effectively been truncated, so there is no cumulative result displayed here, the result is merely 3 hours and 48 minutes.
Enclosing the hour symbol in square brackets (i.e. [h]) instructs Excel to display the cumulative hours of a particular value. Using the current exhibit, we have already established that the difference between the two date and times is 1.158333333. The cumulative difference is thus 1 whole day (24 hours) plus 0.158333333 (3 hours 48 minutes) resulting in 27 hours 48 minutes.
The same principal applies to minutes (m; [m]) and seconds (s; [s]):
|Demonstrating the difference between two times in minutes.|
|Demonstrating the difference between two times in seconds.|
Scale a number by a factor of 10
If the percentage symbol(%) displays a number scaled up by a factor or 100, and the comma symbol (,) displays a number scaled down by a factor of 1000, one can therefore use combinations of these symbols to scale a number by any factor of 10.
The only real downside to the following method is that the percent format always suffixes the number with a percentage symbol. Whilst there is a work-around, it is hardly ideal! The work around in the proceeding examples involves forcing a carriage return before the percentage symbols. This doesn’t remove the percentage symbols but pushes it down to the next line. Thus, if wrap text is turned on for the given cell, and the row height is set only to display 1 line, the percentages appear hidden from view. In these examples, [cr] represents the carriage return. These can be entered in number formats by pressing Control+J.
The following table demonstrates the format method for scaling by a factor of ten. The number of decimal places and such is entirely up to you. I have used 12 decimal places for all numbers only for display purposes. The actual scale display is achieved using the component referred to as the “modifier”.
Using the number 987.654321
|BASE 10 COMPONENT||NUMBER FORMAT||MODIFIER||FUNCTION||FACTOR||RESULT|
There’s a little quirk one should be aware of, so as to not assume that there is something wrong. It seems that after applying some of these formats, when one returns to the format dialog the format code appears different (wrong even). Don’t worry, the format appearance is correct! It appears Excel is confused!
A quick guide to fractions
Format of a fraction: # N/D
# Optional whole number
N Numerator (Integer)
D Denominator (non-zero integer)
Proper Fractions: When the numerator is less than the denominator. E.g.: 3/4
Improper Fractions: When the numerator is greater than the denominator. E.g.: 4/3
Mixed Fractions: When what would otherwise be an improper fraction is represented as the whole part and the remainder expressed as a proper fraction.
Examples representing 2.235 as a fraction:
|NUMBER FORMAT||CELL DISPLAY||PRECISION AS DISPLAYED VALUE|
|Single-digit Mixed Fraction||# ?/?||2 1/4||2.25|
|Double-digit Mixed Fraction||# ?/??||2 4/17||2.235294|
|Triple-digit Mixed Fraction||# ?/???||2 47/200||2.235|
|Mixed Fraction as Halves||# ?/2||2 2||2.235|
|Mixed Fraction as Thirds||# ?/3||2 1/3||2.333333|
|Mixed Fraction as Quarters||# ?/4||2 1/4||2.25|
|Mixed Fraction as Eighths||# ?/8||2 2/8||2.25|
|Mixed Fraction as Tenths||# ?/10||2 2/10||2.2|
The precision displayed is dependent on the number of denominator digits. Looking at the exhibit above, 2.235 is displayed as 2 1/4 if formatted as single-digit fraction. This equates to 2.25, an imprecision if 0.015. This fraction requires triple-digit format to display full precision.
Fixing the significance of the denominator also affects the precision. The factor is of your own choosing, note however in the exhibit how neither halves, thirds, quarters, eighths or tenths can accurately display 2.235 as a fraction.
Colour coding cells according to value
Assume the following:
> All values greater than or equal to 100,000 must appear GREEN
> All values greater than or equal to 50,000 must appear AMBER
> All other values must appear RED
The order of the conditions in this format is of the utmost importance. Because Excel is configured to be efficient, as soon as any condition is met Excel will no longer evaluate the rest of the format.
Colours applied in number format overrule any font colour applied. Thus, should the cell be formatted as red and none or the conditions met, the font will remain red. As soon as a condition is met however the cell font colour will be inherited from the number format.
Use symbols within number formats
There are two ways of including symbols in your number formats.
- Copy the symbol and paste it into the number format code
- Use ALT and type the character code using your number keypad
Here are few:
Display 1, 2 or 3 stars in a cell according to whether or not the cell value is 1, 2 or 3: [<2]"«";[<3]"««";"«««";
The chevron character can be typed into a format by holding ALT and typing 0171 on the numpad.
This format requires that the cell be formatted with Webdings font.
Prefix text with a bullet point to create a bullet list: • @
The bullet character can be typed into a format by holding ALT and typing 0149 on the numpad.
Use the degree symbol to display temperature: # °C
The bullet character can be typed into a format by holding ALT and typing 0176 on the numpad.