VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Changes a decimal number to a fraction up to x/64.

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

Changes a decimal number to a fraction up to x/64.

Rate Changes a decimal number to a fraction up to x/64.




    Dim lngNumerator As Long
    Dim lngWhole As Long
    
    ' Converts decimals to fractions.  Returns max denominator of 64.
    lngWhole = Fix(sglDecimal)
    If lngWhole = 0 Then
        DecimalToFraction = vbNullString
    Else
        DecimalToFraction = CStr(lngWhole) & " "
    End If
    
    lngNumerator = Abs((sglDecimal - lngWhole) / 0.015625)
    
    Select Case lngNumerator
    Case 0
        DecimalToFraction = CStr(lngWhole)
    Case 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62
        lngNumerator = lngNumerator / 2
        DecimalToFraction = DecimalToFraction & CStr(lngNumerator) & "/32"
    Case 4, 12, 20, 28, 36, 44, 52, 60
        lngNumerator = lngNumerator / 4
        DecimalToFraction = DecimalToFraction & CStr(lngNumerator) & "/16"
    Case 8, 24, 40, 56
        lngNumerator = lngNumerator / 8
        DecimalToFraction = DecimalToFraction & CStr(lngNumerator) & "/8"
    Case 16, 48
        lngNumerator = lngNumerator / 16
        DecimalToFraction = DecimalToFraction & CStr(lngNumerator) & "/4"
    Case 32
        lngNumerator = lngNumerator / 32
        DecimalToFraction = DecimalToFraction & CStr(lngNumerator) & "/2"
    Case 64
        DecimalToFraction = CStr(lngWhole + 1)
    Case Else
        DecimalToFraction = DecimalToFraction & CStr(lngNumerator) & "/64"
    End Select
    
End Function

Download this snippet    Add to My Saved Code

Changes a decimal number to a fraction up to x/64. Comments

No comments have been posted about Changes a decimal number to a fraction up to x/64.. Why not be the first to post a comment about Changes a decimal number to a fraction up to x/64..

Post your comment

Subject:
Message:
0/1000 characters