Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Public Function DecimalToFraction(ByVal dNumber As Double, ByVal iDenominator As Integer, sMethod As String) As String
Dim dRes As Double
Dim dPrec As Double
Dim iIn As Long, iParts As Integer
dPrec = 1 / iDenominator 'decimal precision
dRes = 0: iIn = 0: iParts = 0
dRes = Round(dNumber, dPrec, sMethod)
iIn = Int(dRes)
iParts = CInt((dRes - iIn) * iDenominator)
If iParts = iDenominator Then
DecimalToFraction = CStr(iIn + 1)
ElseIf iParts > 0 Then
Do While (iParts Mod 2) = 0 And (iDenominator Mod 2) = 0
iParts = iParts / 2
iDenominator = iDenominator / 2
Loop
If iIn > 0 Then
DecimalToFraction = CStr(iIn) & " " & CStr(iParts) & "/" & CStr(iDenominator)
Else
DecimalToFraction = CStr(iParts) & "/" & CStr(iDenominator)
End If
Else 'parts=0
DecimalToFraction = CStr(iIn)
End If
End Function
Public Function Round(dNumber As Double, dIncrement As Double, sMethod As String) As Double
If sMethod Like "U" Then
Round = CLng((dNumber + dIncrement / 2) / dIncrement) * dIncrement
ElseIf sMethod Like "D" Then
Round = CLng((dNumber - dIncrement / 2) / dIncrement) * dIncrement
Else 'assume nearest
Round = CLng(dNumber / dIncrement) * dIncrement
End If
End Function
Public Function basDec2Fraction(ValIn As Double) As String
'Michael Red 2/6/2002. Tek-Tips Thread "thread222-206890"'
'convert Decimal to Fraction
Dim strDec As String
Dim ValParts() As String
Dim ReducedFract As String
Dim strVal As String
Dim lngNumer As Long
Dim lngDenom As Long
Dim ModOp As Long
Dim Reduce As Boolean
strDec = CStr(ValIn)
ValParts = Split(strDec, ".")
If (UBound(ValParts) <> 1) Then
'Uh Oh! No Decimal to convert.
basDec2Fraction = ValParts(0)
Exit Function
End If
lngNumer = CLng(ValParts(1))
lngDenom = CLng(10 ^ Len(ValParts(1)))
If Len(ValParts(1)) > 0 Then
ModOp = lngNumer / 2
Do While ModOp >= 2
If ((lngNumer Mod ModOp = 0) And (lngDenom Mod ModOp = 0)) Then
lngNumer = lngNumer / ModOp
lngDenom = lngDenom / ModOp
ModOp = lngNumer / 2
Else
ModOp = ModOp - 1
End If
Loop
strVal = ValParts(0) & " and " & Trim(CStr(lngNumer)) & "/" & Trim(CStr(lngDenom))
End If
basDec2Fraction = strVal
End Function