Sub topic 11
Reading and Writing Custom Number Format Codes
This chapter assumes you can apply built-in number formats and know that a format changes how a cell looks, not the value it stores. You should also be comfortable with the Format Cells dialog (Ctrl+1). Here you learn how a custom format code is built, so you can read any code you meet and write your own.
What a custom format code is
A number format code is a short string that tells Excel how to display a stored value. To enter one, select the cells, press Ctrl+1, choose Custom on the Number tab, and type the code in the Type box.
Excel does not change the cell's value. If a cell holds 1234.567 and the format shows 1,235, formulas that reference the cell still use 1234.567. Keep this in mind for the whole chapter.
The four sections
A code can have up to four sections, separated by semicolons:
positive ; negative ; zero ; textExcel picks the section by the value's sign:
| Section | Applies to | Example value |
|---|---|---|
| 1st | Positive numbers | 1500 |
| 2nd | Negative numbers | -1500 |
| 3rd | Zero | 0 |
| 4th | Text | "n/a" |
The number of sections you supply changes the meaning:
- One section applies to every number.
- Two sections: the first covers positive numbers and zero, and the second covers negatives.
- Three sections: positive, negative, and zero.
- Four sections: positive, negative, zero, and text.
In the negative section, Excel does not add a minus sign for you. It shows the value's absolute value, so if you want a sign you must write it. This is why a negative section such as (#,##0) displays -1500 as (1,500).
Digit placeholders
Placeholders decide how many digits appear and what happens when the number has fewer or more digits than the placeholders.
| Symbol | Meaning | Format | Value | Displays |
|---|---|---|---|---|
| `0` | Always shows a digit, padding with 0 | `000.00` | 4.5 | 004.50 |
| `#` | Shows a digit only if it exists | `###.##` | 4.5 | 4.5 |
| `?` | Shows a digit or a space, so decimals align | `0.0?` | 4.5 | 4.5 (with a trailing space) |
| `.` | Decimal point | `0.00` | 4.567 | 4.57 |
| `,` | Thousands separator (between digits) | `#,##0` | 1234567 | 1,234,567 |
Two behaviors cause most mistakes:
- Rounding on display. If the value has more decimals than the format allows, Excel rounds the display. 4.567 with
0.00shows 4.57, but the cell still holds 4.567. - Extra integer digits are never cut. If the format has fewer integer placeholders than the number needs, Excel still shows every integer digit. The value 12345 with
0shows 12345.
Use 0 for the last integer digit (#,##0) so that a value of 0.4 shows as 0 and not as an empty string.
Literal characters and spacing
Some characters appear as written. Others need quotes or a backslash.
- Characters shown without quotes:
$ - + / ( ) : ! ^ & ' ~ { } < > =and the space. - Any other literal text goes inside double quotes, such as
"x"or"FY". - A backslash shows the next single character, so
\xis the same as"x".
Two spacing codes matter in financial models:
_)leaves a blank space the width of a closing parenthesis. The underscore means "pad with the width of the next character."*followed by a character repeats that character to fill the cell width. For example,*-fills with dashes.
The _) code is what keeps columns aligned. Look at these two formats:
#,##0_);(#,##0);"-"_)Positive numbers end with a space as wide as ). Negative numbers end with the real ). Their last digits line up. Without _), positive digits would sit further right than negative digits.
Reading a code step by step
Read a code left to right, one section at a time. Take this code:
#,##0_);(#,##0);"-"_)- Section 1 (
#,##0_)): positive numbers show with thousands separators and no decimals, followed by a space the width of). - Section 2 (
(#,##0)): negative numbers show the absolute value in parentheses. - Section 3 (
"-"_)): zero shows a dash, with the same trailing space.
Here is how stored values display:
| Stored value | Displayed |
|---|---|
| 12500 | 12,500 |
| -12500 | (12,500) |
| 0 | - |
| 0.4 | 0 |
The last row surprises people. The value 0.4 is not zero, so it uses the positive section and rounds to 0. It does not get the dash. Only a value of exactly zero reaches the third section.
Percentages and multiples
The % symbol multiplies the stored value by 100 for display and adds the percent sign. A stored 0.125 with 0.0% shows 12.5%.
0.0%_);(0.0%);"-"_)A multiple (such as EV/EBITDA) needs a literal x. The value is not scaled.
0.0"x"_)This shows 8.25 as 8.3x while the cell still stores 8.25.
Colors and conditions
You can put a color name in square brackets at the start of a section. The available names are [Black], [Blue], [Cyan], [Green], [Magenta], [Red], [White], and [Yellow]. You can also use [Color n] with n from 1 to 56.
#,##0_);[Red](#,##0);"-"_)Conditions in brackets replace the default sign-based rules. With conditions, the sections are tested in order:
[>=1000000]0.0,,"m";[>=1000]0.0,"k";0This shows 2,500,000 as 2.5m, 4,200 as 4.2k, and 85 as 85. A code can contain at most two conditions, and the final section covers everything else. Negative numbers fall into the last section here, so this code suits positive values only.
Scaling with commas
A comma after the last digit placeholder divides the displayed value by 1,000. Each additional comma divides by another 1,000.
#,##0.0,,_);(#,##0.0,,);"-"_)A stored value of 12,345,678 shows as 12.3. The stored number is unchanged, so label the units in a header such as "USD millions". A comma in the middle of the digits (#,##0) is only a separator. A comma at the end is a scaler.
Dates
Date codes use letters instead of digit placeholders.
| Code | Meaning | 31 March 2026 shows as |
|---|---|---|
| `d` / `dd` | Day | 31 |
| `mmm` | Month, short | Mar |
| `mmmm` | Month, full | March |
| `yy` / `yyyy` | Year | 26 / 2026 |
Two examples for period headers:
mmm-yy"FY"yyyyThe first shows Mar-26 and the second shows FY2026. The letter m is ambiguous: next to h or s it means minutes, otherwise it means months.
Writing a format from scratch
Follow this process when you need a new format:
- Decide what each sign should look like: positive, negative, zero.
- Write the positive section first, using
#,##0or0.0%as the base. - Add
_)to the positive and zero sections so they align with the negative parenthesis. - Copy the positive section into the negative section and wrap it in parentheses.
- Replace zero with
"-"_)if you want a dash. - Test with a positive, a negative, a zero, and a small non-zero value.
Common errors
- Forgetting that negatives drop the minus sign. If you write
#,##0;#,##0, negative numbers look positive. - Using
#as the last integer placeholder. A value of 0.4 with#,###shows an empty cell, because no digit is required. - Mistaking a scaler comma for a separator.
#,##0,divides by 1,000. Keep track of where the comma sits. - Changing display but expecting changed values. A scaled or rounded display does not alter sums. A total of cells displayed as 1.2, 1.2, and 1.2 may display 3.7.
- Typing an unquoted letter. Some letters are codes (
d,m,y,h,s,e). Quote literals such as"x".
Exercise
A cell contains -0.0625. Using the format 0.0%_);(0.0%);"-"_), what is displayed, and what does the cell store? What would display if the cell contained exactly 0?
Show answer
The cell displays (6.3%), because the value is negative, uses the second section, is multiplied by 100 by the % symbol, and rounds to one decimal (6.25 rounds to 6.3). The cell still stores -0.0625. If the cell contained exactly 0, the third section applies and it displays a dash followed by a space the width of a closing parenthesis.
Exercise
Write a format that shows values in millions with one decimal, negatives in parentheses, zero as a dash, and the letter "m" after positive and negative values. Then state what 4,560,000 and -4,560,000 display.
Show answer
One answer is #,##0.0,,"m"_);(#,##0.0,,"m");"-"_). The value 4,560,000 displays 4.6m with a trailing space, and -4,560,000 displays (4.6m). Both are divided by one million for display only.