by Waty Thierry (60 Submissions)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Tue 13th April 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Returns a string of a number rounded to a whole and fraction in 16ths, 32nds, etc.
' Returns a string of a number rounded to a whole and fraction in 16ths, 32nds, etc.
' Handles number > 1 and less than 0
' Use as follows:
' Label1.Caption = Dec2Fract(Val(Text1.Text))
' or Label1.Caption = Dec2Fract(Val(Label1.Caption)
Dim F As String, Y As Single, Num As Integer, Den As Integer
Den = 16 ' can be set to 8, 16, 32, 64 etc
If X = 0 Then
Dec2Fract = "0"
Exit Function
Else
Y = Abs(X)
If Y > 1 Then Y = Y - Int(Y) ' get fractional part
Num = CInt(Den * Y)
If Num = Den Then
F = "1"
ElseIf Num = 0 Then
If Abs(X) < 1 Then F = "0" Else F = ""
Else
Do Until Num Mod 2 <> 0
Num = Num / 2
Den = Den / 2
Loop
F = LTrim$(Str$(Num)) + "/" + LTrim$(Str$(Den))
End If
If Abs(X) > 1 Then
If F <> "1" Then
F = Trim$(Str$(Fix(X))) + " " + F
Else
F = Trim$(Str$(CInt(X)))
End If
End If
If X < 0 And X > -1 Then F = "-" + F
Dec2Fract = F
End If
End Function
No comments have been posted about Returns a string of a number rounded to a whole and fraction in 16ths, 32nds, etc.. Why not be the first to post a comment about Returns a string of a number rounded to a whole and fraction in 16ths, 32nds, etc..