|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Re: String.Trim> Consider the following code: It appears that the key question is, what do you plan to do with temp. If > > 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? > 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 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) 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) > > > BK, Actually, you would need to wrap this in a try..catch block as Double.Parse > > All you should need is: > > Dim temp As String = Me.txtAmount.Text > > Return Double.Parse(temp, Globalization.NumberStyles.Currency) 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
To use a combo ornot
Error: Login failed for user ???? Dataset Problem AppDomainSetup: ShadowCopyFiles, ShadowCopyDirectories Example of encryption Printing the contents of a textbox Copy List(Of type) to another List(Of type) How to detect when ShowDialog object has closed? (2003) Base Class Method to use Shadow'ed member variable of Derived Class? How to pass a form control to a class for updating |
|||||||||||||||||||||||