If you need to round a number to a given (variable) number of specified digits or figures, you can do so with an elegant formula that uses the ROUND and LOG10 functions.
In the example shown, the formula in D6 is as follows:
How this formula works
This can be an intimidating formula if you don’t have a good background in math, but let’s work through it step-by-step.
First, when you have a formula like this where one function (in this case ROUND) wraps around all others, it’s often helpful to work from the outside in. So, at the core, this formula is rounding the value in B6 using the ROUND function:
Where x is the number of significant digits required. The tricky part of this formula is calculate x. This is a variable, because it will change depending on the number being rounded. x is calculated with this bit:
This seems complicated, so let’s look first at how the formula needs to work for the given examples. Remember with ROUND that a negative number of digits works on the left side of the decimal. So, to round 1357 to an increasing number of significant digits, we would have:
=ROUND(1357,-6) = 1000 // 1 sig. digit =ROUND(1357,-5) = 1400 // 2 sig. digits =ROUND(1357,-4) = 1360 // 3 sig. digits =ROUND(1357,-3) = 1357 // 4 sig. digits
So, the main problem is how to calculate -6, -5, -4 and so on depending on the number we are rounding.
The key is understanding how these numbers could be expressed using exponents, as in scientific notation:
=ROUND(1357,-6) = 1000 = 1.0*10^6 =ROUND(1357,-5) = 1400 = 1.2*10^6 =ROUND(1357,-4) = 1360 = 1.23*10^6 =ROUND(1357,-3) = 1357 = 1.235*10^6
Note that the exponent is 6 in all cases, which is determined with this bit:
So, the rest of the formula is just using the calculated exponent value to figure out the right number to give ROUND depending on the number of significant digits desired:
=ROUND(1357,-6) // 1-(1+6) = -6 =ROUND(1357,-5) // 2-(1+6) = -5 =ROUND(1357,-4) // 3-(1+6) = -4 =ROUND(1357,-3) // 4-(1+6) = -3
So, in summary:
- ABS converts the value to an absolute (positive) value
- LOG10 gets the exponent, in this case 6 with a decimal value
- INT trims off the decimal part of the exponent
- The formula uses the exponent and the supplied significant digits to figure out the right number of digits to give ROUND
- ROUND rounds the number using the number of digits supplied