Scope
In this brief article we'll see a simple method to convert the decimal part of a number into its fractional representation, developing a reusable class to do it. To that end, we'll use Visual Basic .NET.
Calculate and simplify a fraction
To calculate a fraction, we must separate a number's integral part from the decimal one, to work on the latter to express it in terms of numerator and denominator. Let's take for example the number 12.65. We will first express it as 12 + 0.65, proceeding then in writing our decimal part as the most large non-simplified fraction. Since we have two decimals after the dot, the larger denominator we need is 100. So, we can express our 0.65 as 65/100. Then, using the common rules based on finding the Greatest Common Divisor (GDC), we can simplify our fraction, down to 13/20.
Finding the Greatest Common Divisor
The following is an easy snippet of code to help determine the GDC between two numbers:
- Private Function gcd(ByVal n1 As Integer, ByVal n2 As Integer) As Long
- Dim minimum As Long
- If n1 < n2 Then
- minimum = n1
- Else
- minimum = n2
- End If
- For i As Long = minimum To 1 Step -1
- If n1 Mod i = 0 And n2 Mod i = 0 Then
- Return i
- End If
- Next
- End Function
Calculate a fraction
Here follows the routine that will calculate our fraction. It needs a decimal-type input parameter (as the
12.65 used previously).
- Public Function Calculate(value As Decimal) As String
- Dim intPart As Long = Math.Truncate(value)
- Dim numerator As Long = CType((value - intPart).ToString.Substring(2), Long)
- Dim denominator As Long = CType("1" & StrDup(numerator.ToString.Length, "0"), Long)
-
- Dim _gcd As Long = gcd(numerator, denominator)
- Dim nDiv As Long = _gcd
-
- While nDiv > 1
-
- If numerator Mod nDiv = 0 And denominator Mod nDiv = 0 Then
- numerator /= nDiv
- denominator /= nDiv
- nDiv = _gcd
- Else
- nDiv -= 1
- End If
-
- End While
-
- Dim retVal As String = ""
- If intPart > 0 Then retVal = intPart.ToString & " + ("
- retVal &= numerator.ToString + " / " + denominator.ToString
- If intPart > 0 Then retVal &= ")"
- Return retVal
- End Function
The function will save the integral part of the number for later use, then proceed to calculate the maximum denominator, by adding a number of zeros to equal the number of decimal places. A call to our previously written GCD routine will compute the gretest Common Divisor between our numerator and denominator, entering a loop for dividing the numerator and denominator to determine their common divisors, until no common divisor is available.
Finally, having determined the simplyfied numerator and denominator, the routine will produce their string representation, joining the integral part. So, for our previous example of
value = 12.65, the output will be:
12 + (13/20).
As the reader can note, in case no integral part is present, the fraction will be expressed without parenthesis.
Fraction class
The complete source for a reusable class can be the following:
- Public Class Fraction
-
- Dim _value As Decimal
- Dim _fraction As String
-
- Public ReadOnly Property Value As String
- Get
- Return _fraction
- End Get
- End Property
-
- Public Property Number As Decimal
- Get
- Return _value
- End Get
- Set(value As Decimal)
- _value = value
- _fraction = Calculate(_value)
- End Set
- End Property
-
- Public Sub New(value As Decimal)
- _value = value
- _fraction = Calculate(_value)
- End Sub
-
- Public Sub New()
- _value = 0
- _fraction = 0
- End Sub
-
- Private Function gcd(ByVal n1 As Integer, ByVal n2 As Integer) As Long
- Dim minimum As Long
- If n1 < n2 Then
- minimum = n1
- Else
- minimum = n2
- End If
-
- For i As Long = minimum To 1 Step -1
- If n1 Mod i = 0 And n2 Mod i = 0 Then
- Return i
- End If
- Next
- End Function
-
- Public Function Calculate(value As Decimal) As String
- Dim intPart As Long = Math.Truncate(value)
- Dim numerator As Long = CType((value - intPart).ToString.Substring(2), Long)
- Dim denominator As Long = CType("1" & StrDup(numerator.ToString.Length, "0"), Long)
-
- Dim _gcd As Long = gcd(numerator, denominator)
- Dim nDiv As Long = _gcd
-
- While nDiv > 1
-
- If numerator Mod nDiv = 0 And denominator Mod nDiv = 0 Then
- numerator /= nDiv
- denominator /= nDiv
- nDiv = _gcd
- Else
- nDiv -= 1
- End If
-
- End While
-
- Dim retVal As String = ""
- If intPart > 0 Then retVal = intPart.ToString & " + ("
- retVal &= numerator.ToString + " / " + denominator.ToString
- If intPart > 0 Then retVal &= ")"
- Return retVal
- End Function
-
- End Class
In the source code that comes with that article, I've implemented a simple Windows Forms form, with a TextBox and some Labels, to show how the previous code works.
The code behind will be as simple as this:
- Public Class Form1
-
- Dim f As New Fraction
-
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- f.Number = CType(TextBox1.Text, Decimal)
- Label3.Text = f.Value
- End Sub
- End Class
When the Button is clicked, the Number property of our Fraction class will be initialized with a cast towards Decimal type of what is contained in TextBox1.
Next, the property Value (that will contain our fraction string) will be shown in Label3.
Source Code
The sample code for this article can be downloaded from this
link.