Home All Groups Group Topic Archive Search About
Author
4 Apr 2006 6:17 PM
Jim Wooley
> Consider the following code:
>
> Dim temp As String = Me.txtAmount.Text
> Dim nonNumerics As String
> nonNumerics = "$,"
> temp = temp.Trim(nonNumerics.ToCharArray)
> If txtAmount.Text contains "$1,000.00", shouldn't the
> temp.Trim(nonNumerics.ToCharArray) strip out the comman (,)?  It
> strips out the dollar ($) sign, but not the comma.  I need to pull out
> any nonnumeric characters from the text so that "$1,000.00" becomes
> "1000.00", any other ideas?
>

It appears that the key question is, what do you plan to do with temp. If
you are going to work with it as a number, you might want to consider decimal.Parse
or decimal.TryParse. You will need to use the overload that includes the
NumberStyles option if you need to handle currency.

Jim Wooley
http://devauthority.com/blogs/jwooley/archive/2006/03/15/788.aspx

Author
4 Apr 2006 6:41 PM
BK
Thanks, that did it!

I was trying to return a double, so I it to:

        Dim temp As String = Me.txtAmount.Text
        Dim nonNumerics As String
        nonNumerics = "$,"
        temp = temp.Trim(nonNumerics.ToCharArray)
        Dim returnValue As Double

        Return returnValue.Parse(temp)
Author
4 Apr 2006 7:01 PM
Kerry Moorman
BK,

All you should need is:

     Dim temp As String = Me.txtAmount.Text

     Return Double.Parse(temp, Globalization.NumberStyles.Currency)

Kerry Moorman


Show quoteHide quote
"BK" wrote:

> Thanks, that did it!
>
> I was trying to return a double, so I it to:
>
>         Dim temp As String = Me.txtAmount.Text
>         Dim nonNumerics As String
>         nonNumerics = "$,"
>         temp = temp.Trim(nonNumerics.ToCharArray)
>         Dim returnValue As Double
>
>         Return returnValue.Parse(temp)
>
>
Author
4 Apr 2006 8:14 PM
Jim Wooley
> BK,
>
> All you should need is:
>
> Dim temp As String = Me.txtAmount.Text
>
> Return Double.Parse(temp, Globalization.NumberStyles.Currency)

Actually, you would need to wrap this in a try..catch block as Double.Parse
will throw a format exception if the temp value is not a number. As an alternative,
you could use the following:

Sub Main()
        Console.WriteLine(GetValue(txtAmount.Text).ToString)
        Console.ReadLine()
    End Sub

    Private Function GetValue(ByVal InVal As String) As Double
        Dim temp As Double
        If Double.TryParse(InVal, temp) Then
            Return temp
        Else
            'Return something else to indicate that this is an invalid input
            Return Double.MinValue
        End If
    End Function

Jim Wooley
http://devauthority.com/blogs/jwooley