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:
  1. Private Function gcd(ByVal n1 As Integer, ByVal n2 As Integer) As Long
  2. Dim minimum As Long
  3. If n1 < n2 Then
  4. minimum = n1
  5. Else
  6. minimum = n2
  7. End If
  8. For i As Long = minimum To 1 Step -1
  9. If n1 Mod i = 0 And n2 Mod i = 0 Then
  10. Return i
  11. End If
  12. Next
  13. 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).
  1. Public Function Calculate(value As Decimal) As String
  2. Dim intPart As Long = Math.Truncate(value)
  3. Dim numerator As Long = CType((value - intPart).ToString.Substring(2), Long)
  4. Dim denominator As Long = CType("1" & StrDup(numerator.ToString.Length, "0"), Long)
  5. Dim _gcd As Long = gcd(numerator, denominator)
  6. Dim nDiv As Long = _gcd
  7. While nDiv > 1
  8. If numerator Mod nDiv = 0 And denominator Mod nDiv = 0 Then
  9. numerator /= nDiv
  10. denominator /= nDiv
  11. nDiv = _gcd
  12. Else
  13. nDiv -= 1
  14. End If
  15. End While
  16. Dim retVal As String = ""
  17. If intPart > 0 Then retVal = intPart.ToString & " + ("
  18. retVal &= numerator.ToString + " / " + denominator.ToString
  19. If intPart > 0 Then retVal &= ")"
  20. Return retVal
  21. 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:
  1. Public Class Fraction
  2. Dim _value As Decimal
  3. Dim _fraction As String
  4. Public ReadOnly Property Value As String
  5. Get
  6. Return _fraction
  7. End Get
  8. End Property
  9. Public Property Number As Decimal
  10. Get
  11. Return _value
  12. End Get
  13. Set(value As Decimal)
  14. _value = value
  15. _fraction = Calculate(_value)
  16. End Set
  17. End Property
  18. Public Sub New(value As Decimal)
  19. _value = value
  20. _fraction = Calculate(_value)
  21. End Sub
  22. Public Sub New()
  23. _value = 0
  24. _fraction = 0
  25. End Sub
  26. Private Function gcd(ByVal n1 As Integer, ByVal n2 As Integer) As Long
  27. Dim minimum As Long
  28. If n1 < n2 Then
  29. minimum = n1
  30. Else
  31. minimum = n2
  32. End If
  33. For i As Long = minimum To 1 Step -1
  34. If n1 Mod i = 0 And n2 Mod i = 0 Then
  35. Return i
  36. End If
  37. Next
  38. End Function
  39. Public Function Calculate(value As Decimal) As String
  40. Dim intPart As Long = Math.Truncate(value)
  41. Dim numerator As Long = CType((value - intPart).ToString.Substring(2), Long)
  42. Dim denominator As Long = CType("1" & StrDup(numerator.ToString.Length, "0"), Long)
  43. Dim _gcd As Long = gcd(numerator, denominator)
  44. Dim nDiv As Long = _gcd
  45. While nDiv > 1
  46. If numerator Mod nDiv = 0 And denominator Mod nDiv = 0 Then
  47. numerator /= nDiv
  48. denominator /= nDiv
  49. nDiv = _gcd
  50. Else
  51. nDiv -= 1
  52. End If
  53. End While
  54. Dim retVal As String = ""
  55. If intPart > 0 Then retVal = intPart.ToString & " + ("
  56. retVal &= numerator.ToString + " / " + denominator.ToString
  57. If intPart > 0 Then retVal &= ")"
  58. Return retVal
  59. End Function
  60. 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:
  1. Public Class Form1
  2. Dim f As New Fraction
  3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4. f.Number = CType(TextBox1.Text, Decimal)
  5. Label3.Text = f.Value
  6. End Sub
  7. 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.