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

前端 未结 10 1229
太阳男子
太阳男子 2021-01-05 04:10

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-         


        
相关标签:
10条回答
  • 2021-01-05 04:11

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

    0 讨论(0)
  • 2021-01-05 04:19

    As a very simple display measure, without having to use the rounding function, you can simply change the format of the number and remove 3 significant figures by adding a decimal point after the number.

    I.e. #,###. would show the numbers in thousands. #,###.. shows the numbers in millions.

    Hope this helps

    0 讨论(0)
  • 2021-01-05 04:21

    You could try

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

    value :: The number you wish to round.

    sigfigs :: The number of significant figures you want to round to.

    0 讨论(0)
  • 2021-01-05 04:24

    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.

    0 讨论(0)
  • 2021-01-05 04:25

    You could try custom formatting instead.

    Here's a crash course: https://support.office.com/en-nz/article/Create-a-custom-number-format-78f2a361-936b-4c03-8772-09fab54be7f4?ui=en-US&rs=en-NZ&ad=NZ.

    For three significant figures, I type this in the custom type box: [>100]##.0;[<=100]#,##0

    0 讨论(0)
  • 2021-01-05 04:29

    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.

    0 讨论(0)
提交回复
热议问题