HOWTO: Display a Number as a Fraction

ID: Q185479

The information in this article applies to:

SUMMARY

This article provides three routines to convert a decimal fraction to the form "a b/c".

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

In many instances, it is useful to be able to output a decimal value as a fraction in the form "a/b" or "a b/c". This article provides three routines that perform that function.

Function        Description
--------        -----------

Frac2Num        Returns a normalized fraction with the denominator in the
                range of 2..8.

Frac2NumA       Returns a non-normalized fraction with the denominator you
                specify.

Frac2NumB       Returns a normalized fraction based on a denominator you
                specify.

A normalized fraction contains the smallest possible denominator. The GCF() function (Greatest Common Factor) is used in the normalization process.

Example

Non-normalized : 4/6

Normalized     :  2/3

Sample Code

1. Create a new project and add the following code to a Module:

      Function Num2Frac (ByVal X As Double) As String
      Dim Fixed As Double, Temp As String
        X = Abs(X)
        Fixed = Int(X)
        If Fixed > 0 Then
          Temp = CStr(Fixed)
        End If
        Select Case X - Fixed
          Case .1 To .145
            Temp = Temp + " 1/8"
          Case .145 To .182
            Temp = Temp + " 1/6"
          Case .182 To .225
            Temp = Temp + " 1/5"
          Case .225 To .29
            Temp = Temp + " 1/4"
          Case .29 To .35
            Temp = Temp + " 1/3"
          Case .35 To .3875
            Temp = Temp + " 3/8"
          Case .3875 To .45
            Temp = Temp + " 2/5"
          Case .45 To .55
            Temp = Temp + " 1/2"
          Case .55 To .6175
            Temp = Temp + " 3/5"
          Case .6175 To .64
            Temp = Temp + " 5/8"
          Case .64 To .7
            Temp = Temp + " 2/3"
          Case .7 To .775
            Temp = Temp + " 3/4"
          Case .775 To .8375
            Temp = Temp + " 4/5"
          Case .8735 To .91
            Temp = Temp + " 7/8"
          Case Is > .91
            Temp = CStr(Int(X) + 1)
        End Select
        Num2Frac = Temp
      End Function

      Function Num2FracA (ByVal X As Double, _
                          ByVal Denominator As Long) As String
      Dim Temp As String, Fixed As Double, Numerator As Long
        X = Abs(X)
        Fixed = Int(X)
        Numerator = Int((X - Fixed) * Denominator + .5) ' Rounding
        If Numerator = Denominator Then
          Fixed = Fixed + 1
          Numerator = 0
        End If
        If Fixed > 0 Then Temp = CStr(Fixed)
        If Numerator > 0 Then
          Temp = Trim$(Temp & " " & Numerator & "/" & Denominator)
        End If
        Num2FracA = Temp
      End Function

      Function Num2FracB (ByVal X As Double, _
                          ByVal Denominator As Long) As String
      Dim Temp As String, Fixed As Double, Numerator As Long
      Dim Factor As Long
        X = Abs(X)
        Fixed = Int(X)
        Numerator = Int((X - Fixed) * Denominator + .5)  ' Rounding
        If Numerator = Denominator Then
          Fixed = Fixed + 1
          Numerator = 0
        End If
        If Fixed > 0 Then
          Temp = CStr(Fixed)
        End If
        If Numerator > 0 Then
          Factor = GCF(Numerator, Denominator)  ' Factor for normalization.
          Temp = Trim$(Temp & " " & Numerator / Factor & "/" & _
                       Denominator / Factor)
        End If
        Num2FracB = Temp
      End Function

      Function GCF (ByVal X As Long, ByVal Y As Long) As Long
      '
      ' Returns the Greatest Common Factor.
      ' The largest number that will evenly divide into both X and Y.
      '
      Dim Temp As Long
        X = Abs(X)        ' Make both numbers positive.
        Y = Abs(Y)
        Temp = X Mod Y
        Do While Temp > 0
          X = Y
          Y = Temp
          Temp = X Mod Y
        Loop
        GCF = Y
      End Function

2. Run the project and then press CTRL+BREAK to pause.

3. In the Debug/Immediate window, type the following:

      ?Num2Frac(3.54), Num2FracA(3.54,8), Num2FracB(3.54,8)

   Results:

      3 1/2         3 4/8         3 1/2

REFERENCES

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Malcolm Stewart, Microsoft Corporation

Additional query words: vba kbVBA kbVBp kbdta

Keywords          : kbcode 
Issue type        : kbhowto kbinfo

Last Reviewed: May 19, 1999