How to get excel to display a certain number of significant figures?

105,897

Solution 1

The formula (A2 contains the value and B2 sigfigs)

=ROUND(A2/10^(INT(LOG10(A2))+1),B2)*10^(INT(LOG10(A2))+1)

may give you the number you want, say, in C2. But if the last digit is zero, then it will not be shown with a General format. You have then to apply a number format specific for that combination (value,sigfigs), and that is via VBA. The following should work. You have to pass three parameters (val,sigd,trg), trg is the target cell to format, where you already have the number you want.

Sub fmt(val As Range, sigd As Range, trg As Range)
    Dim fmtstr As String, fmtstrfrac As String
    Dim nint As Integer, nfrac As Integer
    nint = Int(Log(val) / Log(10)) + 1
    nfrac = sigd - nint
    If (sigd - nint) > 0 Then
      'fmtstrfrac = "." & WorksheetFunction.Rept("0", nfrac)
      fmtstrfrac = "." & String(nfrac, "0")
    Else
      fmtstrfrac = ""
    End If
    'fmtstr = WorksheetFunction.Rept("0", nint) & fmtstrfrac
    fmtstr = String(nint, "0") & fmtstrfrac
    trg.NumberFormat = fmtstr
End Sub

If you don't mind having a string instead of a number, then you can get the format string (in, say, D2) as

=REPT("0",INT(LOG10(A2))+1)&IF(B2-(INT(LOG10(A2))+1)>0,"."&REPT("0",B2-(INT(LOG10(A2))+1)),"")

(this replicates the VBA code) and then use (in, say, E2)

=TEXT(C2,D2).

where cell C2 still has the formula above. You may use cell E2 for visualization purposes, and the number obtained in C2 for other math, if needed.

Solution 2

WARNING: crazy-long excel formula ahead

I was also looking to work with significant figures and I was unable to use VBA as the spreadsheets can't support them. I went to this question/answer and many other sites but all the answers don't seem to deal with all numbers all the time. I was interested in the accepted answer and it got close but as soon as my numbers were < 0.1 I got a #value! error. I'm sure I could have fixed it but I was already down a path and just pressed on.

Problem:

I needed to report a variable number of significant figures in positive and negative mode with numbers from 10^-5 to 10^5. Also, according to the client (and to purple math), if a value of 100 was supplied and was accurate to +/- 1 and we wish to present with 3 sig figs the answer should be '100.' so I included that as well.

Solution:

My solution is for an excel formula that returns the text value with required significant figures for positive and negative numbers.

It's long, but appears to generate the correct results according to my testing (outlined below) regardless of number and significant figures requested. I'm sure it can be simplified but that isn't currently in scope. If anyone wants to suggest a simplification, please leave me a comment!

=TEXT(IF(A1<0,"-","")&LEFT(TEXT(ABS(A1),"0."&REPT("0",sigfigs-1)&"E+00"),sigfigs+1)*10^FLOOR(LOG10(TEXT(ABS(A1),"0."&REPT("0",sigfigs-1)&"E+00")),1),(""&(IF(OR(AND(FLOOR(LOG10(TEXT(ABS(A1),"0."&REPT("0",sigfigs-1)&"E+00")),1)+1=sigfigs,RIGHT(LEFT(TEXT(ABS(A1),"0."&REPT("0",sigfigs-1)&"E+00"),sigfigs+1)*10^FLOOR(LOG10(TEXT(ABS(A1),"0."&REPT("0",sigfigs-1)&"E+00")),1),1)="0"),LOG10(TEXT(ABS(A1),"0."&REPT("0",sigfigs-1)&"E+00"))<=sigfigs-1),"0.","#")&REPT("0",IF(sigfigs-1-(FLOOR(LOG10(TEXT(ABS(A1),"0."&REPT("0",sigfigs-1)&"E+00")),1))>0,sigfigs-1-(FLOOR(LOG10(TEXT(ABS(A1),"0."&REPT("0",sigfigs-1)&"E+00")),1)),0)))))

Note: I have a named range called "sigfigs" and my numbers start in cell A1

Test Results:

I've tested it against the wikipedia list of examples and my own examples so far in positive and negative. I've also tested with a few values that gave me issues early on and all seem to produce the correct results.

Results from Wikipedia examples

I've also tested with a few values that gave me issues early on and all seem to produce the correct results now.

3 Sig Figs Test

99.99 -> 100.
99.9 -> 99.9
100 -> 100.
101 -> 101

Notes:

Treating Negative Numbers

To Treat Negative Numbers, I have included a concatenation with a negative sign if less than 0 and use the absolute value for all other work.

Method of construction: It was initially divided into about 6 columns in excel that performed the various steps and at the end I merged all of the steps into one formula above.

Solution 3

Use scientific notation, say if you have 180000 and you need 4 sigfigs the only way is to type as 1.800x10^5

Solution 4

I added to your formula so it also automatically displays the correct number of decimal places. In the formula below, replace the digit "2" with the number of decimal places that you want, which means you would need to make four replacements. Here is the updated formula:

=TEXT(ROUND(A1,2-1-INT(LOG10(ABS(A1)))),"0"&IF(INT(LOG10(ABS(ROUND(A1,2-1-INT(LOG10(ABS(A1)))))))<1,"."&REPT("0",2-1-INT(LOG10(ABS(ROUND(A1,2-1-INT(LOG10(ABS(A1)))))))),""))

For example, if cell A1 had the value =1/3000, which is 0.000333333.., the above formula as-written outputs 0.00033.

Solution 5

This is an old question, but I've modified sancho.s' VBA code so that it's a function that takes two arguments: 1) the number you want to display with appropriate sig figs (val), and 2) the number of sig figs (sigd). You can save this as an add-in function in excel for use as a normal function:

Public Function sigFig(val As Range, sigd As Range)
    Dim nint As Integer
    Dim nfrac As Integer
    Dim raisedPower As Double
    Dim roundVal As Double
    Dim fmtstr As String
    Dim fmtstrfrac As String

    nint = Int(Log(val) / Log(10)) + 1
    nfrac = sigd - nint

    raisedPower = 10 ^ (nint)
    roundVal = Round(val / raisedPower, sigd) * raisedPower

    If (sigd - nint) > 0 Then
        fmtstrfrac = "." & String(nfrac, "0")
    Else
        fmtstrfrac = ""
    End If

    If nint <= 0 Then
        fmtstr = String(1, "0") & fmtstrfrac
    Else
        fmtstr = String(nint, "0") & fmtstrfrac
    End If

    sigFig = Format(roundVal, fmtstr)
End Function

It seems to work in all the use cases I've tried so far.

Share:
105,897
Veridian
Author by

Veridian

Updated on July 05, 2022

Comments

  • Veridian
    Veridian almost 2 years

    I am using excel and i want to display a value to a certain number of significant figures.

    I tried using the following equation

    =ROUND(value,sigfigs-1-INT(LOG10(ABS(value))))
    

    with value replaced by the number I am using and sigfigs replaced with the number of significant figures I want.

    This formula works sometimes, but other times it doesn't.

    For instance, the value 18.036, will change to 18, which has 2 significant figures. The way around this is to change the source formatting to retain 1 decimal place. But that can introduce an extra significant figure. For instance, if the result was 182 and then the decimal place made it change to 182.0, now I would have 4 sig figs instead of 3.

    How do I get excel to set the number of sig figs for me so I don't have to figure it out manually?

  • Veridian
    Veridian over 10 years
    I've never used VBA before, can you give me a quick crash course? I have programming experience so I should be able to fill in some gaps. Where is it, where do I paste the code, how do I use it?
  • sancho.s ReinstateMonicaCellio
    sancho.s ReinstateMonicaCellio over 10 years
    @starbox - I apologize, but it is impossible to give a crash course on VBA here (regardless of it being off-topic). I will try finding a suitable link, and bear in mind that you will have to invest probably more than an hour to get you going with this. You may try the other option in the meantime, and see if it gives the expected results.
  • Veridian
    Veridian over 10 years
    Is using VBA just mean setting up a MACRO?
  • sancho.s ReinstateMonicaCellio
    sancho.s ReinstateMonicaCellio over 10 years
    @starbox - Kind of. But since you will have to pass parameters to your Sub, you will probably have to write another Sub looping through all your target cells.
  • Veridian
    Veridian over 10 years
    Any non VBA solution?
  • sancho.s ReinstateMonicaCellio
    sancho.s ReinstateMonicaCellio over 10 years
    @starbox - Yes, please read starting at "If you don't mind..."
  • Veridian
    Veridian over 10 years
    I'm not sure what to put in for "B4" and "A4"
  • Veridian
    Veridian over 10 years
    =TEXT(val,fmt). Do I have to build something called "fmt" it just gives me an error. Also I tried using 3.568912 for A2 and 3 for B2. It just gave 0.00 as the result using =REPT("0",INT(LOG10(A2))+1)&IF(B2-(INT(LOG10(A2))+1)>0,"."&R‌​EPT("0",B2-(INT(LOG1‌​0(A2))+1)),"")
  • Charlie Gorichanaz
    Charlie Gorichanaz about 8 years
    I'm not sure about this, or I am misunderstanding it. Format ##.0 causes 4141 to display as 4141.0. I can't think of a way to get 4140.
  • ken
    ken over 7 years
    This is an old question, but I've modified sancho.s' comment so that it is a function that takes two arguments: 1) the number you want to display with appropriate sig figs, and 2) the number of sig figs. You can save this as an add-in function:
  • Steve
    Steve over 7 years
    To format text with no decimal point or decimal places, use TEXT([value], "0"). For example, TEXT(-0.6, "0") is -1, TEXT(-0.5, "0") is 0, TEXT(0.4, "0") is 0, TEXT(0.5, "0") is 1, etc. This can produce leading zeroes too; TEXT(15, "000") is 015.
  • coderforlife
    coderforlife over 3 years
    To make this work for values >=10 with 3 sig figs I have to change the < to <=. However I think the more general solution is more like <2-1 where 2 becomes ones of the values to replace.