textbox format to decimal?
i have a textbox which i required it to be of either a currency or decimal value.
amtvalue = FormatNumber$(Val(amttb.text, 2)
had been using this formula to do the checking and converting and i realise tat i can't realli fully convert the string into a decimal.
example:
if value of amttb.text is "dsa.32", output for amtvalue will be 0.00
however, if amttb.text is "123.fsdf", output for amtvalue will be 123.00
is there another way for mi to do the check and make it such tat even if the input is "123.fsdf", the output will still be 0.00?
Re: textbox format to decimal?
vb.net Code:
Private Sub Button2_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button2.Click
Dim str As String = "adc.123"
'Dim str As String = "123.adc"
Dim value As Single
Single.TryParse(str, value)
MessageBox.Show(value.ToString("0.00"))
End Sub
Re: textbox format to decimal?
Code:
'if value of amttb.text is "dsa.32", output for amtvalue will be 0.00
'however, if amttb.text is "123.fsdf", output for amtvalue will be 123.00
Dim testV() As String = New String() {"dsa.32", "123.fsdf", "123.12"}
Dim amttb As New TextBox
Dim amtvalue As Decimal
For x As Integer = 0 To testV.Length - 1 'do the testV
amttb.Text = testV(x)
If Decimal.TryParse(amttb.Text, amtvalue) Then
Debug.WriteLine("number " & amtvalue.ToString("C"))
Debug.WriteLine("number " & amtvalue.ToString("F2"))
Else
Debug.WriteLine("not number " & amtvalue.ToString("C"))
Debug.WriteLine("not number " & amtvalue.ToString("F2"))
End If
Next