VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



will convert a double to an engineering format (i.e. p, n, u, m, K, M, G, T) to a given number of s

by R Wilcock (1 Submission)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 4th April 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

will convert a double to an engineering format (i.e. p, n, u, m, K, M, G, T) to a given number of significant figures

Rate will convert a double to an engineering format (i.e. p, n, u, m, K, M, G, T) to a given number of s



    Dim tmpResult As String
    Dim unit As String
    Dim n As Integer
    Dim value As Double
    
    tmpResult = CStr(Abs(dblNumber))
    If intSF > Len(tmpResult) Then intSF = Len(tmpResult)
    
    n = 0
    
    value = dblNumber
    
    If value >= 10 Then
        Do Until value < 10
        n = n + 1
        value = value / 10
        Loop
    Else
        Do Until value >= 1
        n = n - 1
        value = value * 10
        Loop
    End If
    
    If n >= 15 Then
        MsgBox "Number formatting failed", vbExclamation
    ElseIf n >= 12 Then
        unit = unit & "T"
        n = n - 12
    ElseIf n >= 9 Then
        unit = unit & "G"
        n = n - 9
    ElseIf n >= 6 Then
        unit = unit & "M"
        n = n - 6
    ElseIf n >= 3 Then
        unit = unit & "K"
        n = n - 3
    ElseIf n >= 0 Then
        unit = unit & ""
    ElseIf n >= -3 Then
        unit = unit & "m"
        n = n + 3
    ElseIf n >= -6 Then
        unit = unit & "u"
        n = n + 6
    ElseIf n >= -9 Then
        unit = unit & "n"
        n = n + 9
    ElseIf n >= -12 Then
        unit = unit & "p"
        n = n + 12
    ElseIf n >= -15 Then
        MsgBox "Number formatting failed", vbExclamation
    End If

    tmpResult = CStr(Abs(value))
    tmpResult = Left$(tmpResult, intSF)
    value = CDbl(tmpResult)
    
    Do Until n = 0
        n = n - 1
        value = value * 10
        Loop
    
    tmpResult = CStr(Abs(value))
    tmpResult = Left$(tmpResult, intSF)
    
    HumanReadableWithSF = tmpResult & unit

End Function

Download this snippet    Add to My Saved Code

will convert a double to an engineering format (i.e. p, n, u, m, K, M, G, T) to a given number of s Comments

No comments have been posted about will convert a double to an engineering format (i.e. p, n, u, m, K, M, G, T) to a given number of s. Why not be the first to post a comment about will convert a double to an engineering format (i.e. p, n, u, m, K, M, G, T) to a given number of s.

Post your comment

Subject:
Message:
0/1000 characters