0

Hi everyone, I'm new in vb.Net programming and i just want to ask how to validate a "Double" data type.

for example.

```
Dim number as Double
number = textbox1.text
```

If the value of "number" variable is in "Double" data type like 1.23 or 0.12

it returns a correct value and If the value of of a "number" is like ... or ..

it returns an error

```
Conversion from string "." to type 'Double' is not valid.
```

I tried to suppres the error using the try catch but it still return the same error

This Question has been **Answered**

0

hi ddanbe.

No sir I didn't replace the decimal point by a comma. there is a different between comma(,) and dot(.) in checking a "Double" data type.

I trying to input this wrong value for the "Double" data type.

0.123.456 - this is wrong

This value is no longer "Double" data type. purpose is Im trying to solve this bug if the user input a wrong value or format.

0.123456 - this is right

*Edited
by decade*

1

Don't really get what you are trying to achieve here. If your textbox contains things like "..." and you try to convert that to a double, you will get an error. You could try to intercept the keys that get pressed and only allow one decimal point. Look here for info.

1

Here is the solution to your problem. First, to solve the conundrum presented by DDanbe, you need to use a MaskedTextbox and set a custom mask using as many numbers and you want on the left then a decimal point and then as many as you want on the right. If you don't know how to do this then look it up. Next you have to parse the input from string to strings to numbers to number like so:

Notably, the code is ugly because I wrote it fast for an example, but it works (and that's what matters). Check it out and it will show you how to parse input to validate that you have a double and then use that double in some way (I used a msgbox to display it). Anyway, hope this helps.

```
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim num_len As Integer = 0 ' set by length of whole numbers
Dim per_len As Integer = 0 ' set by length of decimal numbers
Dim A As Integer = 0 ' place holder
Dim str_input As String = TextBox1.Text
Dim _Count As Integer = str_input.Length
Dim str_number(_Count) As String ' I don't know big of a number you want to go up to
Dim str_Decimal As String ' Same as above
Dim str_Percent(_Count) As String ' Use this to hold numbers after the "."
For X As Integer = 0 To _Count - 1 ' Parse the whole numbers out now
If (str_input(X) <> ".") Then ' Parse the whole numbers and then the decimal point
str_number(X) = str_input(X)
Else
num_len = X
GoTo PARSE_POINT
End If
If (0 = 1) Then
PARSE_POINT:
str_Decimal = str_input(X) ' 'parse the decimal point out now
Dim dec_place As Integer = X
For Y As Integer = 0 To (_Count - (X + 1)) ' parse the percentage portion out now (Count is total and X + 1 is whole numbers and decimal already done)
str_Percent(Y) = str_input(Y + dec_place) ' only use the numbers after the decimal point
per_len = Y
Next
End If
Next
Dim number_str As String
Dim percent_str As String
For T As Integer = 0 To num_len ' set whole numbers to 1 string
number_str += str_number(T)
Next
For G As Integer = 0 To per_len ' set decimal numbers to another string
percent_str += str_Percent(G)
Next
' OK, now the strings are parsed and we can just convert each to a number and then add those numbers together
Dim new_number As Double = Convert.ToDouble(number_str)
Dim new_percent As Double = Convert.ToDouble(percent_str)
new_percent /= 10 ^ A ' convert the whole number into its correct percentage of 1
Dim total_number As Double = new_number + new_percent
MsgBox(total_number)
End Sub
```

1

And here's a quick alteration I did to show how you could parse out spaces. You could use this same kind of technique to parse out anything you don't want as input:

```
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim num_len As Integer = 0 ' set by length of whole numbers
Dim per_len As Integer = 0 ' set by length of decimal numbers
Dim str_input As String = TextBox1.Text ' holds user input provided from MaskedInputBox
Dim _Count As Integer = str_input.Length ' total provided input string length
Dim str_number(_Count) As String ' maybe the length is all whole numbers?
Dim str_Decimal As String ' Same as above
Dim str_Percent(_Count) As String ' maybe the length is all decimal numbers? Holds numbers after "." is found
For X As Integer = 0 To _Count - 1 ' Parse the whole numbers out now
If (str_input(X) <> "." And str_input(X) <> " ") Then ' Parse the whole numbers and remove spaces then the decimal point
str_number(X) = str_input(X)
ElseIf (str_input(X) = ".") Then
num_len = X ' whole numbers equals how many iterations before we found "."
GoTo PARSE_POINT
End If
If (0 = 1) Then
PARSE_POINT:
str_Decimal = str_input(X) ' 'parse the decimal point out now
Dim dec_place As Integer = X ' the decimal point is found where the 'X' For stopped iterating to come here
For Y As Integer = 0 To (_Count - (X + 1)) ' parse the percentage portion out now (Count is total and X + 1 is where the decimal numbers start)
If (str_input(Y + dec_place) <> " ") Then
str_Percent(Y) = str_input(Y + dec_place) ' only use the numbers after the decimal point
per_len = Y ' Y is equal to number of decimals processed
End If
Next
End If
Next
Dim number_str As String
Dim percent_str As String
For T As Integer = 0 To num_len ' set whole numbers to 1 string
number_str += str_number(T)
Next
For G As Integer = 0 To per_len ' set decimal numbers to another string
percent_str += str_Percent(G)
Next
' OK, now the strings are parsed and we can just convert each to a number and then add those numbers together
Dim new_number As Double = Convert.ToDouble(number_str)
Dim new_percent As Double = Convert.ToDouble(percent_str)
Dim total_number As Double = new_number + new_percent
MsgBox(total_number)
End Sub
End Class
```

*Edited
by Jamblaster*: Correction

This question has already been answered. Start a new discussion instead.

Recommended Topics

Hi guys, can you help me with a MIPS Asm exercise.

It's the first time that i coding in this language...

I solved the exercise in Java, you can get ...