Results 1 to 3 of 3

Thread: Two Numbers After Decimal Point !

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    30

    Wink Two Numbers After Decimal Point !

    Hi, I need to make 5 as 5.00 , .5 as 0.50 , etc.
    I tried
    Code:
    Textbox1.Text = Math.Round (Textbox1.Text,1)
    And
    Code:
    Textbox1.Text = Format(Textbox1.text.Tostring,"#.##")
    But Not Working Help Please !

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Two Numbers After Decimal Point !

    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"))

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Two Numbers After Decimal Point !

    Quote Originally Posted by .paul. View Post
    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:
    Code:
    TextBox1.Text = Val(TextBox1.Text).ToString("f2")
    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:
    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width