converting decimals to display as fractions

D

Deb

I need to convert decimals to display as fractions in my
forms and reports. Eg: 0.50 as 1/2 etc. I've searched all
over and hope someone can assist me.

Thanks in advance for your time.
 
G

Glenn

Cut and paste the below into a module and give it a try.


Function Num2Frac(x)
'
' Converts a decimal number to a normalized fraction, and
automatically
' determines a Denominator between 2 and 8.
'
Dim Temp As String, Fixed As Double
If (VarType(x) < 2) Or (VarType(x) > 6) Then
Num2Frac = x
Else
x = Abs(x)
Fixed = Int(x)
If Fixed > 0 Then
Temp = Str(Fixed)
End If
Select Case x - Fixed
Case Is < 0.1
If Fixed > 0 Then
Temp = Temp
Else
Temp = Str(x)
End If
Case 0.1 To 0.145
Temp = Temp + " 1/8"
Case 0.145 To 0.182
Temp = Temp + " 1/6"
Case 0.182 To 0.225
Temp = Temp + " 1/5"
Case 0.225 To 0.29
Temp = Temp + " 1/4"
Case 0.29 To 0.35
Temp = Temp + " 1/3"
Case 0.35 To 0.3875
Temp = Temp + " 3/8"
Case 0.3875 To 0.45
Temp = Temp + " 2/5"
Case 0.45 To 0.55
Temp = Temp + " 1/2"
Case 0.55 To 0.6175
Temp = Temp + " 3/5"
Case 0.6175 To 0.64
Temp = Temp + " 5/8"
Case 0.64 To 0.7
Temp = Temp + " 2/3"
Case 0.7 To 0.775
Temp = Temp + " 3/4"
Case 0.775 To 0.8375
Temp = Temp + " 4/5"
Case 0.8735 To 0.91
Temp = Temp + " 7/8"
Case Is > 0.91
Temp = Str(Int(x) + 1)
End Select
Num2Frac = Temp
End If
End Function
 
G

Guest

Thanks heaps Glenn.

I had to insert a new field into my form and then use the control source as your script and the field name I wanted to convert. It works beautifully and you did good!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top