Last post Feb 17, 2017 07:26 PM by Rion Williams
Feb 17, 2017 06:29 PM|Dragnorian|LINK
Hello, I have searched countless threads and websites and none of them answer my problem I can not figure out. Anyways, I am developing a program that basically converts the amount of numbers in between numbers in like tenths or hundredths... Say 1 to 0
in tenths. 10 counts. Okay? I have that down but I added a custom number option but I do not know how to convert the input(they type it in a text box) into a decimal. Say they input 5, I want the program to read that as 0.5 or if they choose 4829, I want the
program to read that as .4829 . I do not know how to do this. I was testing it with a MessageBox.Show(test) and I did something like Dim test As Decimal = 5 and the msgbox output was 5, not 0.5 . Any suggestions?
Feb 17, 2017 07:26 PM|Rion Williams|LINK
If you always want the decimal equivalent of the value that you are entering, you could simply append a "0." prior to parsing it since it's going to be coming from a string :
Public Shared Function GetFractionalAmount(n As String) As Decimal
' Trim off any existing leading decimal formatting '
n = System.Text.RegularExpressions.Regex.Replace(n, "^/d*\.", "")
Return [Decimal].Parse(Convert.ToString("0.") & n)
So you would basically always pass your TextBox value into there to get your decimal result :
Decimal decimalFraction = GetFractionalAmount(YourTextBox.Text)