Sub topic 12
Rounding results and nesting functions
This chapter assumes you can write SUM, AVERAGE, COUNT, MIN, and MAX over a range, and that you know how IFERROR replaces an error with a message. Here you will learn to control the precision of a result with ROUND, and to combine functions by placing one inside another.
Why rounding matters
Aggregate functions often return long decimals. Suppose B2:B6 holds 10, 20, 25, 30, and 33. The average is 23.6, which is tidy. Change the last value to 34 and the average is 23.8. Now change the data to 10, 20, 25, 30, and 32 and the average is 23.4. Real data rarely behaves this politely. An average of three prices such as 1.10, 2.20, and 2.30 is 1.866666666666667, and Sheets stores that full value.
You have two ways to shorten a number:
- Formatting changes only how the cell looks. The stored value stays the same.
- ROUND changes the stored value. Every formula that refers to the cell uses the rounded number.
The difference shows up when you add up displayed values. Three cells may each show 0.33 while holding 0.3333. Their sum displays as 1.00, but the three displayed numbers add to 0.99. If you want the visible numbers and the totals to agree, round the values themselves.
The ROUND family
ROUND takes a number and the count of decimal places to keep.
=ROUND(23.456, 2)This returns 23.46. The second argument is called places. Its meaning depends on its sign:
| places | Effect | `ROUND(1234.567, places)` |
|---|---|---|
| 2 | Keep two decimals | 1234.57 |
| 0 | Round to a whole number | 1235 |
| -1 | Round to the nearest ten | 1230 |
| -2 | Round to the nearest hundred | 1200 |
If you leave out places, Sheets uses 0. A negative value rounds to the left of the decimal point, which is useful for reporting large totals in tens or hundreds.
ROUND sends a value ending in exactly 5 away from zero. ROUND(2.5, 0) returns 3 and ROUND(-2.5, 0) returns -3. This is the rule most people learn at school. Some accounting systems use "round half to even", which gives 2 for 2.5. Sheets does not do that, so totals may differ by a cent from such systems.
Two related functions force the direction:
=ROUNDUP(23.451, 2)
=ROUNDDOWN(23.459, 2)ROUNDUP returns 23.46 and ROUNDDOWN returns 23.45. Both move toward or away from zero rather than toward the nearest value. Use ROUNDUP when you must never under-order, for example when you compute how many boxes you need. Use ROUNDDOWN when you must never overstate, such as whole units you can actually ship.
Nesting: a function inside a function
A function can take another function as an argument. This is called nesting. Sheets evaluates the inner function first and passes its result to the outer one.
=ROUND(AVERAGE(B2:B6), 2)Sheets works in this order:
AVERAGE(B2:B6)computes the mean, for example 23.6666666667.ROUND(23.6666666667, 2)returns 23.67.
Read nested formulas from the inside out. The innermost call runs first, and its result replaces it in the next call up.
Each function has its own parentheses, and each argument list must close. In =ROUND(AVERAGE(B2:B6), 2), the first ) closes AVERAGE. The , 2 belongs to ROUND, so it sits after that bracket. Putting the 2 inside AVERAGE gives AVERAGE(B2:B6, 2), which silently averages the range together with the number 2 and returns a wrong answer without any error.
Nesting with IFERROR
Chapter material on errors showed that AVERAGE over a range with no numbers returns #DIV/0!. You can combine rounding and error handling by choosing which function is outer.
=IFERROR(ROUND(AVERAGE(B2:B6), 2), "No data")Here AVERAGE runs first. If it fails, the error passes through ROUND, and IFERROR catches it and shows "No data". If it succeeds, ROUND shortens the number and IFERROR passes it along unchanged.
Putting IFERROR on the inside changes the behaviour:
=ROUND(IFERROR(AVERAGE(B2:B6), "No data"), 2)When the range is empty, IFERROR returns the text "No data", and ROUND then tries to round text, which produces #VALUE!. The error moves rather than disappearing. As a rule, put IFERROR on the outside so it can catch anything that goes wrong beneath it.
Also keep in mind that IFERROR hides every error, including ones caused by typos in a reference. Use it for the failures you expect, such as an empty range, and check the formula without it while you build it.
Nesting aggregates inside each other
Any of the five functions can appear as an argument to another. Some useful combinations:
=MAX(B2:B6) - MIN(B2:B6)
=ROUND(SUM(B2:B6) / COUNT(B2:B6), 2)
=MAX(0, SUM(B2:B6))- The first gives the range of the data, the gap between the largest and smallest value.
- The second reproduces AVERAGE by hand, which helps when you need to change the divisor. For example,
SUM(B2:B6) / COUNTA(B2:B6)divides by all non-empty cells and so treats text as if it were a zero. - The third uses MAX as a floor. If the sum is negative, the result is 0.
You can also use functions inside arithmetic. =SUM(B2:B6) * 0.2 computes 20 percent of a total, and =ROUND(SUM(B2:B6) * 0.2, 2) rounds that amount to cents.
Round at the right moment
When you chain calculations, rounding early can accumulate error. Consider a price list where each item is 0.333 and you sell 3 of them.
=ROUND(0.333, 2) * 3
=ROUND(0.333 * 3, 2)The first returns 0.99 because 0.333 became 0.33 before the multiplication. The second returns 1.00 because the multiplication used the full value. Neither is wrong in general, but they answer different questions. If a single item is really sold at 0.33, the first is correct. If 0.333 is the true unit cost, the second is.
Follow these guidelines:
- Keep full precision in intermediate cells and round only in the cell that presents the final answer.
- Round at each step only when the business rule says each step produces a rounded amount, such as a tax line that is billed in cents.
- When totals must match displayed line items, round each line item and then sum the rounded cells.
Floating-point surprises
Sheets stores numbers in binary, so some decimals cannot be represented exactly. A comparison such as =(0.1 + 0.2) = 0.3 can return FALSE in some programs, because the sum is stored as a number very slightly above 0.3. Sheets hides much of this by comparing close values as equal, but long chains of arithmetic can still leave tiny remainders. If you test whether a computed total equals an expected value, wrap the total in ROUND first:
=ROUND(SUM(B2:B6), 2) = 100This compares the total at cent precision and avoids false mismatches.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Formatting a cell to two decimals and assuming totals use two decimals | Displayed lines do not add up to the displayed total | Use ROUND on the values |
| `=ROUND(B2:B6, 2)` | Sheets returns an array of rounded values spilling down, or only the first, depending on context, but not a rounded total | Round the aggregate: `=ROUND(SUM(B2:B6), 2)` |
| Placing the `places` argument inside the inner function | Wrong result with no error | Close the inner function's bracket first |
| Using IFERROR inside ROUND | `#VALUE!` replaces `#DIV/0!` | Make IFERROR the outer function |
| Rounding every step of a long calculation | Drift from the true value | Round once, at the end |
Worked example
A shop records daily takings in B2:B8. Some days are blank because the shop was closed. You want the average per open day, rounded to cents, with a clear message if nothing was recorded.
=IFERROR(ROUND(AVERAGE(B2:B8), 2), "No data")AVERAGE ignores the blank cells, so closed days do not pull the mean down. If takings were 120.50, 98.25, and 143.10 on three days, the sum is 361.85, the average is 120.616666..., and the formula displays 120.62. If every cell is blank, AVERAGE fails and the cell shows "No data".
Add a second cell for the spread between the best and worst day:
=MAX(B2:B8) - MIN(B2:B8)With the figures above this returns 143.10 - 98.25, which is 44.85.
Exercise
Cells B2:B5 contain 4.10, 4.20, 4.30, and 4.45. Write one formula that returns the average rounded to one decimal place and shows "Empty" if B2:B5 held no numbers. Then say what the formula returns for these values.
Show answer
Use =IFERROR(ROUND(AVERAGE(B2:B5), 1), "Empty"). The sum is 17.05 and the average is 4.2625, so the formula returns 4.3. IFERROR is the outer function so it can catch the #DIV/0! that AVERAGE gives for a range with no numbers.
Exercise
A cell contains =ROUND(SUM(B2:B6) / COUNT(B2:B6), 2). B2:B6 holds 5, 10, "n/a", 15, and a blank cell. What does it return, and how would the result change if COUNT were replaced by COUNTA?
Show answer
SUM adds 5, 10, and 15 to get 30. COUNT counts only the three numbers, so the result is 30 / 3 = 10. COUNTA counts the four non-empty cells, including the text "n/a", so the result would be 30 / 4 = 7.5.
Exercise
Why does =ROUND(IFERROR(AVERAGE(B2:B6), "No data"), 2) still show an error when B2:B6 is empty?
Show answer
AVERAGE fails, so IFERROR returns the text "No data". ROUND then receives text instead of a number and returns #VALUE!. Putting IFERROR on the outside, as in =IFERROR(ROUND(AVERAGE(B2:B6), 2), "No data"), catches the error.