Hi Kelvin,
As no-one has come up with anything better, I'm happy to explain.
To get a number to
n significant figures is simply a rounding exercise, hence the
ROUND function; this gives us ..
[blue][tt] ROUND(A1, ...)[/tt][/blue]
The ... needs to be a number. Normally people use rounding to round digits after the decimal point, e.g.
[tt] ROUND(0.456,2)[/tt] rounds to 2 decimal places (the nearest multiple of 10 ^ -2), giving 0.46
[tt] ROUND(0.456,1)[/tt] rounds to 1 decimal place (the nearest multiple of 10 ^ -1), giving 0.4
It is also possible to use negative numbers to round before the decimal point, e.g.
[tt] ROUND(456,[red]-[/red]2)[/tt] rounds to 2 decimal places (the nearest multiple of 10 ^ -(-2)), giving 500
To round to a particular significance depending on the number (i.e
n significant figures) we need to find out how many significant digits there are to start with in order to feed the right number into the ROUND Function. For this we use logarithms (the
LOG10 Function).
A brief reminder from your schooldays: the logarithm of a (positive) number consists of two parts; the integer part which is the largest power of 10 lower than the number, and the decimal part indicating where the number falls in the range between that and the next highest power of 10. The integer part is what we are interested in here because it tells us where the first significant digit is, and for this we use the
INT Function. An examples ..
[tt] LOG10(456) [/tt] = 2.65896, and ..
[tt] INT(LOG10(456))[/tt] = 2, telling us that the number falls between 10 ^ 2 and 10 ^ 3
It is slightly more complex for numbers less than 1; the logarithm of 0.0456 is (mathematically) -2 +0.65896 and the integer part of this is -2. This doesn't appear quite so obviously from an example, but the INT Function moves away from zero so the result is what we want.
[tt] LOG10([red]0.0[/red]456) [/tt] = -1.34104, and ..
[tt] INT(LOG10([red]0.0[/red]456))[/tt] = -2, telling us that the number falls between 10 ^ -2 and 10 ^ -1
Adding 1 to the integer part of the log gives us the number of digits before the decimal point so, with the above examples ..
[tt] INT(LOG10(456))[/tt] = 2; add 1 and we find there are 3 digits before the point
[tt] INT(LOG10([red]0.0[/red]456))[/tt] = -2; add 1 and we find there are -1 digits before the point (in other words there +1 zeroes after the point before we get to significant digits)
Back to the ROUND Function. Rounding to
n significant figures means rounding to the position
n digits after the first significant figure (which is the number of digits before the point which we got from the Log above), in other words ..
[tt] INT(LOG10(number)+1) - n[/tt]
Because the ROUND Function requires the negative of this, what we actually feed into it is the reverse of the above ..
[tt] n - (INT(LOG10(A1))+1)[/tt]
(where A1 is the cell containing the original number)
Putting it together, and using 4 significant figures as example, we get ..
[tt] ROUND(A1,4-(INT(LOG10(A1))+1))[/tt]
Finally, to cater for negative numbers, we ignore the sign in the process of determining the length. This is achieved by using the
ABS Function, which gives the magnitude (or absolute value) of a number. Adding this into the mix gives the final formula of ..
[tt] ROUND(A1,4-(INT(LOG10(ABS(A1)))+1))[/tt]
Finally, the MAX Function. I left this in by mistake and am surprised you find it works with it but not without. It only has effect for very small numbers, as per this example
[tt] ROUND(A1,4-(INT(LOG10(ABS(0.00045678)))+1,0))[/tt] should give 0.0004568 (i.e. 4 significant figures after all the zeroes after the decimal point), and ..
[tt] ROUND(A1,4-(MAX(INT(LOG10(ABS(A1)))+1,0)))[/tt] should give 0.0005 (i.e. 4 digits after the decimal point regardless of significance)
Sorry about the rather long post. It looks like I got carried away. I hope it helps and, an absolute final note, I hope I've got all the closing parentheses in the right places
Enjoy,
Tony
--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.