Create a new module and paste this source code into it. You should name this module basMath. If you have downloaded issue #19 you can add this code to it. If you have any questions, email us at help@codeoftheweek.com
'----------------------------------------------------------------------
'
' Module Name: basMath
' Written By: C&D Programming Corp.
' Create Date: 7/99
' Copyright: Copyright 1999 by C&D Programming Corp. Source
' code may not be reproduced except for use in a
' compiled executable. All rights reserved. If
' you would like to reprint any or all of this
' code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit
'
' Find the Greatest Common Divisor using Euclid's algorithm.
'
Public Function GCD(ByVal a As Long, ByVal b As Long) As Long
Dim T As Long
' while b is not zero, continue searching...
While (b)
T = a Mod b
a = b
b = T
Wend
GCD = a
End Function
'
' For best results you should pass in a Decimal data type by using the CDec function. This
' will preserve the decimal places in the most accurate manner.
'
Public Function DecimalToFraction(decDecimal As Variant) As String
Dim lWholePortion As Long
Dim decFractionPortion As Variant
Dim sFraction As String
Dim lNumberOfDecimals As Long
Dim lGCD As Long
Dim lDenominator As Long
Dim lNumerator As Long
' first get the whole number portion and figure out the decimal portion
lWholePortion = Fix(decDecimal)
decFractionPortion = CDec(decDecimal) - lWholePortion
' convert the decimal to a string which will yield a format of 0.xxx
' we just take the length of this string and subtract two (one for the 0 and the
' other for the .)
sFraction = CStr(decFractionPortion)
lNumberOfDecimals = Len(sFraction) - 2
If lNumberOfDecimals > 9 Then
Err.Raise -5, "DecimalToFraction", "Too many decimal places."
End If
' calculate the numerator and denominator and then the greatest common divisor
lDenominator = (10 ^ lNumberOfDecimals)
lNumerator = lDenominator * decFractionPortion
lGCD = GCD(lNumerator, lDenominator)
' if the whole portion of the number is not zero and any part of the fraction is less than
' zero, let's remove the negative so that the final fraction looks right. This will
' be executing in cases such as -1.99.
If (lNumerator < 0 Or lDenominator < 0 Or lGCD < 0) And (lWholePortion <> 0) Then
lNumerator = Abs(lNumerator)
lDenominator = Abs(lDenominator)
lGCD = Abs(lGCD)
End If
' do not show the zero if the whole portion of the number is 0
DecimalToFraction = IIf(lWholePortion = 0, "", lWholePortion & " ") & _
(lNumerator / lGCD) & "/" & (lDenominator / lGCD)
End Function