Question How to extract fraction from single Text Box

CNC_Man

Member
Joined
Dec 5, 2009
Messages
16
Programming Experience
Beginner
How would I extract a fraction "3/8" from a single text box so I can convert them back to a decimal??
Thank you so much for any help.
 
The Text of a TextBox is a String. You can call String.Split to split on the division operator and get the two numeric values. Convert them both to actual numbers and then perform the division.
 
The Text of a TextBox is a String. You can call String.Split to split on the division operator and get the two numeric values. Convert them both to actual numbers and then perform the division.
Thanks for the reply but would you be so kind to elaborate further?
 
Nope. You have all you need. Read the instructions, make an attempt to follow them and then post back if you have a specific issue along the way. If you don't know how to perform a particular step then research that specifically. For instance, have made any effort to learn how to use String.Split? We're not here to teach you the basics or help you avoid doing any research of your own. This is a relatively simple process, I've told you what the steps are and each step is very simple. I'm confident that you are able to take from here, but you need to actually try.
 
Almost made me mad for you to post a reply like that. Old Dog and new tricks related LOL. But I stand corrected! I actually way over thought that code and since you really made me mad(LOL) I found the solution to be way more easy.
In case someone looks at this. Here you go.

Dim str = TbResult.Text

Dim split = str.Split("/")

Dim numerator As String
Dim denominator As String

If (split.Count = 2) Then
numerator = split(0).ToString()
denominator = split(1).ToString()

TbDecimalFromFract.Text = numerator / denominator
End If

I know this can be converted but in this case there is no need.

THANK YOU!!
 
Almost made me mad for you to post a reply like that. Old Dog and new tricks related LOL. But I stand corrected! I actually way over thought that code and since you really made me mad(LOL) I found the solution to be way more easy.
In case someone looks at this. Here you go.

Dim str = TbResult.Text

Dim split = str.Split("/")

Dim numerator As String
Dim denominator As String

If (split.Count = 2) Then
numerator = split(0).ToString()
denominator = split(1).ToString()

TbDecimalFromFract.Text = numerator / denominator
End If

I know this can be converted but in this case there is no need.

THANK YOU!!
One thing to keep in mind is the possibility of bad input from the user and before doing any math on anything gotten from the textbox you should make sure what they put in can be converted to a decimal and you can use Decimal.TryParse() for that after splitting the string on the "/" character:
Dim Fraction As String() = TbResult.Text.Trim().Split("/"c)
If Fraction.Length = 2I Then
    Dim Numerator, Denominator As Decimal

    If Decimal.TryParse(Fraction(0I), Numerator) Then
        If Decimal.TryParse(Fraction(1I), Denominator) Then
            TbDecimalFromFract.Text = (Numerator / Denominator).ToString()
        Else
            TbDecimalFromFract.Text = "Invalid Denominator"
        End If
    Else
        TbDecimalFromFract.Text = "Invalid Numerator"
    End If
End If
 
One thing to keep in mind is the possibility of bad input from the user and before doing any math on anything gotten from the textbox you should make sure what they put in can be converted to a decimal and you can use Decimal.TryParse() for that after splitting the string on the "/" character:
Dim Fraction As String() = TbResult.Text.Trim().Split("/"c)
If Fraction.Length = 2I Then
    Dim Numerator, Denominator As Decimal

    If Decimal.TryParse(Fraction(0I), Numerator) Then
        If Decimal.TryParse(Fraction(1I), Denominator) Then
            TbDecimalFromFract.Text = (Numerator / Denominator).ToString()
        Else
            TbDecimalFromFract.Text = "Invalid Denominator"
        End If
    Else
        TbDecimalFromFract.Text = "Invalid Numerator"
    End If
End If

Absolutely! Thank you. In this case it is not a user input but rather something calculated already. User only inputs a decimal Value. "Now that statement could be confusing" LOL This app calculates nearest fraction in 64ths, 32nds, 16ths, 8ths,4ths and 2nds and displays the result in a text box, from there i needed a way to calculate the rounded fraction back to a decimal so I can display the difference between the user input and calculated value from the fraction in decimal.
I use that in the machine shop for the draftings that give decimal hole sizes.
And thanks for the reply!
 
Back
Top