13,003,094 members (57,290 online)
Tip/Trick
alternative version

#### Stats

8.2K views
2 bookmarked
Posted 10 Jun 2010

# Decimal To Fraction

, 10 Jun 2010
 Rate this:
Converts a given decimal number to fraction
```Function dec2frac(ByVal dblDecimal As Double) As String
Dim intNumerator, intDenominator, intNegative As Integer
Dim dblFraction, dblAccuracy As Double
Dim txtDecimal As String

dblAccuracy = 0.1                                       ' Set the initial Accuracy level.
txtDecimal = dblDecimal.ToString                        ' Get a  string representation of the input number.

For i As Integer = 0 To (txtDecimal.Length - 1)                                 ' Check each character to see if it's a decimal point...
If txtDecimal.Substring(i, 1) = "." Then                    ' if it is then we get the number of digits behind the decimal
dblAccuracy = 1 / 10 ^ (txtDecimal.Length - i)    '   assign the new accuracy level, and
Exit For                                            '   exit the for loop.
End If
Next
intNumerator = 0                                ' Set the initial numerator value to 0.
intDenominator = 1                              ' Set the initial denominator value to 1.
intNegative = 1                                 ' Set the negative value flag to positive.
If dblDecimal < 0 Then
intNegative = -1 ' If the desired decimal value is negative,
End If

dblFraction = 0                                 ' Set the fraction value to be 0/1.

Do While Math.Abs(dblFraction - dblDecimal) > dblAccuracy   ' As long as we're still outside the
'   desired accuracy, then...
If Math.Abs(dblFraction) > Math.Abs(dblDecimal) Then      ' If our fraction is too big,
intDenominator += 1         '   increase the denominator
Else                                            ' Otherwise
intNumerator += intNegative   '   increase the numerator.
End If

dblFraction = intNumerator / intDenominator     ' Set the new value of the fraction.

Loop

Return intNumerator.ToString & "/" & intDenominator.ToString ' Display the numerator and denominator
End Function
Function num(ByVal dblDecimal As Double) As String
Dim intNumerator, intDenominator, intNegative As Integer
Dim dblFraction, dblAccuracy As Double
Dim txtDecimal As String

dblAccuracy = 0.1                                       ' Set the initial Accuracy level.
txtDecimal = dblDecimal.ToString                        ' Get a  string representation of the input number.

For i As Integer = 0 To (txtDecimal.Length - 1)                                 ' Check each character to see if it's a decimal point...
If txtDecimal.Substring(i, 1) = "." Then                    ' if it is then we get the number of digits behind the decimal
dblAccuracy = 1 / 10 ^ (txtDecimal.Length - i)    '   assign the new accuracy level, and
Exit For                                            '   exit the for loop.
End If
Next
intNumerator = 0                                ' Set the initial numerator value to 0.
intDenominator = 1                              ' Set the initial denominator value to 1.
intNegative = 1                                 ' Set the negative value flag to positive.
If dblDecimal < 0 Then
intNegative = -1 ' If the desired decimal value is negative,
End If

dblFraction = 0                                 ' Set the fraction value to be 0/1.

Do While Math.Abs(dblFraction - dblDecimal) > dblAccuracy   ' As long as we're still outside the
'   desired accuracy, then...
If Math.Abs(dblFraction) > Math.Abs(dblDecimal) Then      ' If our fraction is too big,
intDenominator += 1         '   increase the denominator
Else                                            ' Otherwise
intNumerator += intNegative   '   increase the numerator.
End If

dblFraction = intNumerator / intDenominator     ' Set the new value of the fraction.

Loop

Return intNumerator.ToString
End Function
Function den(ByVal dblDecimal As Double) As String
Dim intNumerator, intDenominator, intNegative As Integer
Dim dblFraction, dblAccuracy As Double
Dim txtDecimal As String

dblAccuracy = 0.1                                       ' Set the initial Accuracy level.
txtDecimal = dblDecimal.ToString                        ' Get a  string representation of the input number.

For i As Integer = 0 To (txtDecimal.Length - 1)                                 ' Check each character to see if it's a decimal point...
If txtDecimal.Substring(i, 1) = "." Then                    ' if it is then we get the number of digits behind the decimal
dblAccuracy = 1 / 10 ^ (txtDecimal.Length - i)    '   assign the new accuracy level, and
Exit For                                            '   exit the for loop.
End If
Next
intNumerator = 0                                ' Set the initial numerator value to 0.
intDenominator = 1                              ' Set the initial denominator value to 1.
intNegative = 1                                 ' Set the negative value flag to positive.
If dblDecimal < 0 Then
intNegative = -1 ' If the desired decimal value is negative,
End If

dblFraction = 0                                 ' Set the fraction value to be 0/1.

Do While Math.Abs(dblFraction - dblDecimal) > dblAccuracy   ' As long as we're still outside the
'   desired accuracy, then...
If Math.Abs(dblFraction) > Math.Abs(dblDecimal) Then      ' If our fraction is too big,
intDenominator += 1         '   increase the denominator
Else                                            ' Otherwise
intNumerator += intNegative   '   increase the numerator.
End If

dblFraction = intNumerator / intDenominator     ' Set the new value of the fraction.

Loop

Return intDenominator.ToString
End Function```

## About the Author

 Student India
No Biography provided

## Comments and Discussions

 First Prev Next
 Thanks! Willy Kimura27-Dec-15 0:04 Willy Kimura 27-Dec-15 0:04
 Last Visit: 31-Dec-99 18:00     Last Update: 27-Jun-17 2:38 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.