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

前端 未结 10 1322
太阳男子
太阳男子 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条回答
  •  萌比男神i
    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.

提交回复
热议问题