Results 1 to 3 of 3

Thread: textbox format to decimal?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    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?
    Thanks,

    Ron

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: textbox format to decimal?

    vb.net Code:
    1. Private Sub Button2_Click( _
    2.     ByVal sender As System.Object, _
    3.     ByVal e As System.EventArgs _
    4. ) Handles Button2.Click
    5.  
    6.     Dim str As String = "adc.123"
    7.     'Dim str As String = "123.adc"
    8.     Dim value As Single
    9.  
    10.     Single.TryParse(str, value)
    11.     MessageBox.Show(value.ToString("0.00"))
    12. End Sub

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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