12,815,971 members (38,531 online)
Technical Blog
Add your own
alternative version

#### Stats

19.2K views
6 bookmarked
Posted 20 Feb 2012

# How to Convert a Fraction to a Decimal

, 20 Feb 2012 CPOL
 Rate this:
Please Sign up or sign in to vote.
Converting a fraction to a decimal

This little test program has a lot of user input validation.

About 10 years ago, while working as a pipefitter, I was called upon to figure out how to fit a riser pipe onto a reducer. Since the reducer is tapered, there was no normal way to do it.

I had to calculate as a best guess as to what my dimensions would be based on what I already knew about fitting pipe to pipe. It didn’t turn out near as pretty as I would have hoped, but a grinder does wonders to finish getting it closer to where it needed to be.

While I’m currently laid off, even though I haven’t had to do one of these since then, I finally decided to set down and figure out all of the math involved to create the 8 points and lengths needed for half of the riser circle and to be able to add the differences in the taper of the reducer. It took about 3 days working on and off to complete the formula to do all of the calculations required.

Do to the massive amount of calculations required to output the final 8 lengths, I wanted to create a program that does all of the math for the user.

My first hurdle is the user input validation. I need to validate if the person is inputting a decimal, a fraction or some off the wall entry.

Since the normal user would input a fraction, I searched for a premade function on the internet for the fraction to decimal conversion. I found this one here from a March 14, 2008 blog post. You find very few that go from fraction to decimal. Most samples go from decimal to fraction.

After removing an extra `EndIf`, the code worked fine for converting a fraction to a decimal (see code below).

```Private Function FractionToDecimal(ByVal frac As String) As String
Dim decimalVal As String = "0"
Dim upper As Decimal = 0
Dim lower As Decimal = 0
Dim remain As Decimal = 0
If frac.IndexOf("/") <> -1 Then

'End If < ----Extra End if
If frac.IndexOf(" ") <> -1 Then
remain = CType(frac.Substring(0, frac.IndexOf(" ")), Decimal)
frac = frac.Substring(frac.IndexOf(" "))
End If
upper = CType(frac.Substring(0, frac.IndexOf("/")), Decimal)
lower = CType(frac.Substring(frac.IndexOf("/") + 1), Decimal)
decimalVal = (remain + (upper / lower)).ToString
End If
Return decimalVal
End Function```

After getting the code to work, I then started thinking about user input validation. I added validation for every type of malformed input I could think of and gave the ability to return an error or return the result of the conversion.

As you can see, the first screenshot is a normal conversion. The second screenshot shows the fraction with the enumerator larger than the denominator, I could have allowed this type of input, but in this case I am catching for when people swap the two numbers.

### Now the Final Code

```Imports System.Text.RegularExpressions

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.Text = "Convert Fraction to Decimal  " & My.Application.Info.Version.ToString
End Sub

Private Sub btnConvert_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnConvert.Click
'Starts at the begining and checks for any upper or lower case letters.
Dim regx As New Regex("^[a-zA-Z]")
' Regx class http://msdn.microsoft.com/en-us/library/
'                  system.text.regularexpressions.regex(v=vs.90).aspx
Dim inputNfo As String
' The String from the input text box
inputNfo = tbUserInput.Text
Dim outputStr As String = Nothing
'Here we check if it is a reconised decimal number,
'if true then just return the decimal number
If IsNumeric(inputNfo) = True Then
outputStr = inputNfo
tbOutput.ForeColor = Color.Green
lblInputValidation.Text = "Input is Valid"
lblInputValidation.ForeColor = Color.Green
' Verify user is not spelling out the number
ElseIf regx.IsMatch(inputNfo) = True Then
outputStr = "Please check you are not Spelling your whole number"
lblInputValidation.Text = "Input is Not Valid"
lblInputValidation.ForeColor = Color.Red
' if not a decimal, verify it is not a malformed fraction
ElseIf inputNfo.Contains("/") And inputNfo.Contains(".") = False Then
Dim ReturnString As String
ReturnString = FractionToDecimal(inputNfo)
'this part checks in the function if the enumerator is larger than the denominator
If ReturnString = "Error Please Check Fraction" Then
outputStr = ReturnString
tbOutput.ForeColor = Color.Red
lblInputValidation.Text = "Input is Not Valid"
lblInputValidation.ForeColor = Color.Red
Else
'If all is good then return the converterd fraction as a decimal
outputStr = ReturnString
tbOutput.ForeColor = Color.Green
lblInputValidation.Text = "Input is Valid"
lblInputValidation.ForeColor = Color.Green
End If

'this checks that the input in not blank
ElseIf inputNfo = Nothing Or inputNfo = " " Then
outputStr = "Nothing was input"
tbOutput.ForeColor = Color.Red
lblInputValidation.Text = "Nothing was input"
lblInputValidation.ForeColor = Color.Red
Else
'returns this if the input number is realy messed up.
outputStr = "Input not reconised"
tbOutput.ForeColor = Color.Red
lblInputValidation.Text = "Input not reconised "
lblInputValidation.ForeColor = Color.Red
End If

'Returns the final output
tbOutput.Text = outputStr.ToString
End Sub

Private Function FractionToDecimal(ByVal frac As String) As String
'Code from http://amrelgarhytech.blogspot.com/2008/03/fraction-to-decimal.html
',,Fixed error to get to work.
Dim decimalVal As String = "0"
Dim upper As Decimal = 0
Dim lower As Decimal = 0
Dim remain As Decimal = 0
If frac.IndexOf("/") <> -1 Then

If frac.IndexOf(" ") <> -1 Then
remain = CType(frac.Substring(0, frac.IndexOf(" ")), Decimal)
frac = frac.Substring(frac.IndexOf(" "))
End If

End If
upper = CType(frac.Substring(0, frac.IndexOf("/")), Decimal)
lower = CType(frac.Substring(frac.IndexOf("/") + 1), Decimal)
If upper > lower Then
Return "Error Please Check Fraction"
Else
decimalVal = (remain + (upper / lower)).ToString
End If

Return decimalVal
End Function

End Class```

Starting from the top, we need the “`Imports System.Text.RegularExpressions`” for checking user input in the second test.

Next, we set `inputNfo` as a `string` and equal it to the input text.

Then, we run the `inputNfo` through the VB.NET built-in `IsNumeric` function to see if it is a recognized decimal number, if it is, then just return that number to the output textbox. No conversion needs to be done.

If `inputNfo` is not recognized as a number, we then pass it to the regular expression test and make sure the user is not trying to spell out the number (three instead of 3), if they are, it returns an error, if not, then it passes on to the next test.

The next test would be to find out if the character “`/`” is in the `string` and also to make sure the character “`.`” is not in the string, if it is, then the user input looks something like:

“22. 3/4” , with the decimal in place, it will return an error.

We could add more code to handle that problem and fix it but, why baby the user, make them input the information correctly so they don’t get used to inputting the information incorrectly.

If it passes all of those tests, then the string / number gets passed to the actual function that does the conversion. While in the conversion function we verify that the fraction does not have a larger enumerator than a denominator. Our users should not need to input the fraction in this way. (For the final program that this sample app. was built to test.)

If it fails the test, then it returns an error message, (as in the screenshot above). If it passes, then it returns the result of the Conversion as a decimal.

### Conclusion

Again, due to the massive amount of calculations, my final program will require, I need to validate every possible way a user could input a number so the end result would be a decimal, and then I would be able to do the calculations on the input parameters.

If I catch all possible input errors, then any output errors should be easier to trace and fix.

The output of the 8 lengths in my final application will then be converted back from a decimal to a fraction rounded to the nearest 1/16th of an inch.

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## About the Author

 CEO PC's Xcetra United States
My first experience with computers was when my mom gave a Timex Sinclair 1000 to me for Christmas some time in the late 70's (I still have it)There I learned to copy code from magazines to save to cassette tapes for playing games.

Since then I have dabbled in:
Basic,Qbasic,ruby,python,Java Script, HTML, CSS, C#, C++, Perl, and a few other I can't think of off hand.
Now I Mainly work with VB Script and VB.Net
I Prefer to build programs that make use of the GUI so I don't have to remember all of the syntax for console apps. I realy don't care much for HTML because of the way you build and then run to see if it looks right. Also the new WPF is to much like HTML so I steer clear of it for now.
Most of what I build is for getting information from a system to use in system repair.I make heavy use of the WMI classes. Why reinvent something.

## Comments and Discussions

 First Prev Next
 More concise version Brent Wiens4-Jul-14 8:42 Brent Wiens 4-Jul-14 8:42
 Re: More concise version ledtech34-Jul-14 8:54 ledtech3 4-Jul-14 8:54
 My vote of 5 manoj kumar choubey14-Mar-12 1:49 manoj kumar choubey 14-Mar-12 1:49
 Nice
 Re: My vote of 5 ledtech31-Jul-12 5:20 ledtech3 1-Jul-12 5:20
 Last Visit: 31-Dec-99 19:00     Last Update: 23-Mar-17 8:42 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 20 Feb 2012
Article Copyright 2012 by ledtech3
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid