# How To Display a Number as a Fraction

Expand all | Collapse all

## SUMMARY

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

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 a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS 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

163435 VBA: Programming Resources for Visual Basic for Applications

## Properties

Article ID: 185479 - Last Review: November 23, 2006 - Revision: 2.3
##### APPLIES TO
• Microsoft Visual Basic for Applications 5.0
##### Keywords:
kbcode kbhowto kbinfo KB185479
Retired KB Content Disclaimer