String to Integer conversion

DekaFlash

Well-known member
Joined
Feb 14, 2006
Messages
117
Programming Experience
1-3
How do i force the int() part of variable1 = Int(textbox1.text) to default to decimal conversion?
 
You should use:
VB.NET:
        If IsNumeric(TextBox1.Text) Then
            variable1 = Convert.ToDecimal(TextBox1.Text)
        Else
            MessageBox.Show("Must be a numeric value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
If you need to force decimal places you should look into the ToString method.
eg. ToString("c") for standard currency, ToString("n2") for two decimal places, etc.

Otherwise "17" is still just 17 as a decimal when displayed. So if you want it to be 17.00 then you would have:
VB.NET:
        If IsNumeric(TextBox1.Text) Then
            variable1 = Convert.ToDecimal(TextBox1.Text)
            Dim tempstring As String = variable1.ToString("n2")
            variable1 = Convert.ToDecimal(tempstring)
        Else
            MessageBox.Show("Must be a numeric value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
 
Last edited:
Back
Top