Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Finding the Number of Significant Digits.
Written by Allen Wyatt (last updated April 6, 2024)
This tip applies to Excel 97, 2000, 2002, and 2003
Brenda is interested in knowing the number of significant digits in a value. She wonders if there is an Excel function or formula she can use that would return the number of significant digits in the value shown in a cell.
This question is not as simple as it seems. For some people, finding the number of digits in a value, less any decimal points or negative signs. If that is all you need, then something like this formula will work just fine:
=IF(A1<0,IF(A1=INT(A1),LEN(A1)-1,LEN(A1)-2),IF(INT(A1)=A1,LEN(A1),LEN(A1)-1))
The reason that this isn't that simple, however, is because what constitutes the number of significant digits in a value depends on many things. The bottom line is that you can't always tell by looking at a value how many significant digits it has.
For instance, the value 100 could have 1, 2, or 3 significant digits. It is presumed that the value 1.00 has 3 significant digits, but that may not be the case if the value displayed is the result of formatting imposed by Excel—for instance, the value in the cell could be 1.0000437, which Excel formats as 1.00. You can discover more about the topic of significant digits here:
http://excel.tips.net/T001983
There are some generally accepted ways to identify significant digits in a number, but any attempt to codify a set of rules is always open to debate. One such set of rules has been noted at Wikipedia, in the "Identifying Significant Digits" section of this article:
http://en.wikipedia.org/wiki/Significant_figures
With at least a rudimentary set of rules in mind (such as the one in the Wikipedia article) it is possible to develop a user-defined function that will give you the most likely number of significant digits for a value.
Function SigFigs(rng As Range, Optional iType As Integer = 1) 'iType = 1 is Min 'iType = 2 is Max Dim rCell As Range Dim sText As String Dim sText2 As String Dim iMax As Integer Dim iMin As Integer Dim iDec As Integer Dim i As Integer Application.Volatile Set rCell = rng.Cells(1) 'if not a number then error If Not IsNumeric(rCell) Or IsDate(rCell) Then SigFigs = CVErr(xlErrNum) Exit Function End If sText2 = Trim(rCell.Text) sText = "" 'find position of decimal point (it matters) iDec = InStr(sText2, ".") 'strip out any non-numbers (including decimal point) For i = 1 To Len(sText2) If Mid(sText2, i, 1) >= "0" And _ Mid(sText2, i, 1) <= "9" Then _ sText = sText & Mid(sText2, i, 1) Next 'remove any leading zeroes (they don't matter) While Left(sText, 1) = "0" sText = Mid(sText, 2) Wend iMax = Len(sText) 'strip trailing zeroes (they don't matter if no decimal point) sText2 = sText If iDec = 0 Then While Right(sText2, 1) = "0" sText2 = Left(sText2, Len(sText2) - 1) Wend End If iMin = Len(sText2) 'return Min or Max Select Case iType Case 1 SigFigs = iMin Case 2 SigFigs = iMax Case Else SigFigs = CVErr(xlErrNum) End Select End Function
You call this function by using the following in your worksheet:
=SigFigs(A1, x)
You can replace x with either 1 or 2. If you specify 1, then the function returns the minimum number of significant digits. If you specify 2, then the function returns the maximum number of significant digits. In most cases the two possible return values will be the same, except with values that are whole numbers, without a trailing decimal point, that have trailing zeroes. In other words, if you use the function to evaluate the number 1234000, then the minimum (x is 1) returns 4 and the maximum (x is 2) returns 7.
The function takes into consideration how the number appears in the worksheet, meaning that it matters how the number is formatted. It strips out any formatting characters, such as negative signs, parentheses, and commas.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10975) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Finding the Number of Significant Digits.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
After customizing your Excel toolbars, it is a good idea to make a backup of the file that contains the information. ...
Discover MoreIf your arrow keys and the Enter key aren't working as you expect them to, the problem could have any number of causes. ...
Discover MoreMove from one worksheet to another, and Excel selects whatever cell was last used in the worksheet you are selecting. If ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-04-07 06:19:42
Peter
Significant figures and precision of a calculation are dependent on the precision of the starting values/arguments. Quoting the article Allen suggested "Spurious digits that arise from calculations resulting in a higher precision than the original data or a measurement reported with greater precision than the instrument's resolution." This probably applies to a large number of calculations in Excel. As the article suggests rounding is a way to prevent reporting of excessive precision. Working out the rounding is another problem.
Relative error is a fair way to assess precision. Say you know that for a measurement "A" the relative error is ±α, and for B it is ±β. It is likely that A could vary over A*(1±α). Then the product A*B is close to A*B*(1±(α+β)), and the ratio is close to A/B(1±(α+β)). For addition or subtraction it is a bit messier. The value y=A*B or y=A/B should be rounded by y*(α+β) if that were possible.
I can't find a function in Excel that gives calculates significance. Say the result is 77.53 accurate to 2%. That is, (α+β) =0.02 in my examples. 2% of 77.53 is ~0.19, so the result could fall between ~77.31 and ~77.69. I would argue that 77.53 has 3 significant figures in this case.
2024-04-06 06:44:17
jamies
Associated with the problem of significant digits is the way floating point values are stored, and the frequency with which Excel uses that to store results of multiply and divide.
Plus the limitations -
as in add 1 to a large number such as 1234567890543210 and the value does not change.
When testing numeric values for equality it is better to check the remainder of subtracting the 2 is not significant
as in
IF(ABS(val1-val2)<0.0000001,"near enough","different")).
the formula provided
=IF(A1<0,IF(A1=INT(A1),LEN(A1)-1,LEN(A1)-2),IF(INT(A1)=A1,LEN(A1),LEN(A1)-1))
could, perhaps benefit from the use of ABS() and perhaps a test for actually being numeric not a string, or void
Maybe also in the script cater for the radix point being a "," rather than a "."
And, the comment that leading zeroes don't matter if no decimal point - well, if it has been stripped out ?
I would have "counted the characters up to, but excluding the decimal point character, then the count after , using a max of the number of decimal places to count -
that because of the stored value of such as 1/3 and of 0.2
Basically those will always be 16, unless the value is larger than 10^16, when the extra zeroes before the decimal point are significant !
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments