VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Ever wanted a computer to format numbers based on Significant Digits? Here's a function to do it

by Joel Cassel (1 Submission)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 20th April 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Ever wanted a computer to format numbers based on Significant Digits? Here's a function to do it

Rate Ever wanted a computer to format numbers based on Significant Digits? Here's a function to do it



  ByVal intDigits As Integer) As Double
  
    Dim dFactor As Double
    Dim dStart As Double
    Dim Bneg As Boolean
    Dim msg As String
    Dim mg As Integer
    
On Error GoTo ER:
    
    If num = 0 Then
        Sig_Digits = 0
        Exit Function
    End If
    If num < 0 Then
        dStart = num * -1
        Bneg = True
    Else
        dStart = num
    End If
    
    dFactor = 1E+99
    Do Until dFactor = 1E-99
        If dStart >= dFactor / 10 And dStart < dFactor Then
            dStart = dStart / dFactor
            dStart = Round(dStart, intDigits)
            dStart = dStart * dFactor
            If Bneg = True Then dStart = dStart * -1
            Exit Do
        End If
        dFactor = dFactor / 10
    Loop
    
    Sig_Digits = dFactor
    Exit Function
ER:
    msg = "Significant Digits Function Error"
    mg = MsgBox(msg, vbExclamation, App.Title)
    Sig_Digits = 0
    Exit Function
End Function

Download this snippet    Add to My Saved Code

Ever wanted a computer to format numbers based on Significant Digits? Here's a function to do it Comments

No comments have been posted about Ever wanted a computer to format numbers based on Significant Digits? Here's a function to do it. Why not be the first to post a comment about Ever wanted a computer to format numbers based on Significant Digits? Here's a function to do it.

Post your comment

Subject:
Message:
0/1000 characters