Hi, I need to make 5 as 5.00 , .5 as 0.50 , etc.
I triedAndCode:Textbox1.Text = Math.Round (Textbox1.Text,1)
But Not Working Help Please ! :confused:Code:Textbox1.Text = Format(Textbox1.text.Tostring,"#.##")
Printable View
Hi, I need to make 5 as 5.00 , .5 as 0.50 , etc.
I triedAndCode:Textbox1.Text = Math.Round (Textbox1.Text,1)
But Not Working Help Please ! :confused:Code:Textbox1.Text = Format(Textbox1.text.Tostring,"#.##")
the text property of a textbox is a string. you can't round, or format a string like that:
Code:Textbox1.Text = val(Textbox1.text.Tostring("f2"))
You got your parentheses a bit wrong there. That should be:That's not necessarily the best option though. Val is going to create a number no matter what but it may not be the number that the user intended. For instance, if the user enter "1o2" instead of "102" then Val will produce the number 1. If what you're actually trying to do is format the number in the TextBox when the user leaves that field then I'd suggest this:Code:TextBox1.Text = Val(TextBox1.Text).ToString("f2")
Code:Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Not Double.TryParse(Me.TextBox1.Text, Nothing) Then
MessageBox.Show("Please enter a valid number.")
'Don't let the user move to a different field until they enter valid data.
e.Cancel = True
End If
End Sub
Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
'We know that the data is valid.
Me.TextBox1.Text = Double.Parse(Me.TextBox1.Text).ToString("f2")
End Sub