VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Converts fractions to decimals (x/2, x/4, x/8, x/16, x/32, x/64). Modifications can be made for oth

by Mark S. (5 Submissions)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 15th February 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Converts fractions to decimals (x/2, x/4, x/8, x/16, x/32, x/64). Modifications can be made for other denominators.

Rate Converts fractions to decimals (x/2, x/4, x/8, x/16, x/32, x/64). Modifications can be made for oth




    Dim intCount As Integer
    Dim lngDenominator As Long
    Dim lngNumerator As Long
    Dim lngRetCode As Long
    Dim lngWhole As Long
    Dim strTemp As String
    
    FractionToDecimal = 0#
    strFraction = Trim$(strFraction)
    
    If strFraction > vbNullString Then
        For intCount = 1 To Len(strFraction)
            Select Case Asc(Mid$(strFraction, intCount, 1))
            Case 32, 45, 47 To 58 ' space, minus, divide, and 0 to 9
            Case Else
                If IsNumeric(strFraction) Then
                    FractionToDecimal = CSng(strFraction)
                End If
                Exit Function
            End Select
        Next intCount
        
        lngRetCode = InStr(strFraction, "/")
        If lngRetCode > 0 Then
            lngRetCode = InStr(strFraction, " ")
            If lngRetCode > 0 Then
                lngWhole = CLng(Left$(strFraction, lngRetCode - 1))
                strTemp = Right$(strFraction, Len(strFraction) - lngRetCode)
            Else
                lngWhole = 0
                strTemp = strFraction
            End If
            
            lngRetCode = InStr(strTemp, "/")
            If lngRetCode > 0 Then
                lngNumerator = CLng(Left$(strTemp, lngRetCode - 1))
                lngDenominator = CLng(Right$(strTemp, Len(strTemp) - lngRetCode))
                If lngDenominator <> 0 Then
                    If lngWhole < 0 Then
                        FractionToDecimal = lngWhole - (lngNumerator / lngDenominator)
                    Else
                        FractionToDecimal = lngWhole + (lngNumerator / lngDenominator)
                    End If
                End If
            End If
        Else
            FractionToDecimal = CSng(strFraction)
        End If
    End If

End Function

Download this snippet    Add to My Saved Code

Converts fractions to decimals (x/2, x/4, x/8, x/16, x/32, x/64). Modifications can be made for oth Comments

No comments have been posted about Converts fractions to decimals (x/2, x/4, x/8, x/16, x/32, x/64). Modifications can be made for oth. Why not be the first to post a comment about Converts fractions to decimals (x/2, x/4, x/8, x/16, x/32, x/64). Modifications can be made for oth.

Post your comment

Subject:
Message:
0/1000 characters