lordofduct
Well-known member
- Joined
- Jun 24, 2010
- Messages
- 71
- Programming Experience
- 3-5
I'm writing my own parsing method to parse a String to a double. I have a lot of requirements of this parsing method, so none of the existing parsing methods actually work.
Here are the requirements:
Like TryParse, on most failures we should return 0. Things like stack overflow and the sort still bubble out.
Unlike TryParse, I don't want to pass the return value as a reference. Instead it just returns the value... like say Convert.ToDouble(...) does.
Accept optional NumberStyles in case the user wants to modify that.
must be able to support leading/trailing whitespace (the strings come from xml so there is no guarantee there isn't leading/trailing whitespace)
must be able to support hex values dynamically... meaning if the numeric value in the string starts with 0x, #, or &H... it automatically drops into hex mode.
Is numeric greedy, meaning that it will read through the string up until it hits the last numeric value and convert that... just cutting off any dangling none numeric values. For instance " 12zzz" return 12 because it just slices off the zzz
anyways, this works... it's about 20-25 times slower than regular old Double.TryParse... as is expected (I use Regex and the sort). Wonder if anyone has any suggestions to speeding it up. I don't need it to be on par with Double.TryParse or even near it... that'd be ridiculous as I'm expecting it to do a lot more than TryParse even does. But I'd like to try and speed it up just a little bit more.
Here are the requirements:
Like TryParse, on most failures we should return 0. Things like stack overflow and the sort still bubble out.
Unlike TryParse, I don't want to pass the return value as a reference. Instead it just returns the value... like say Convert.ToDouble(...) does.
Accept optional NumberStyles in case the user wants to modify that.
must be able to support leading/trailing whitespace (the strings come from xml so there is no guarantee there isn't leading/trailing whitespace)
must be able to support hex values dynamically... meaning if the numeric value in the string starts with 0x, #, or &H... it automatically drops into hex mode.
Is numeric greedy, meaning that it will read through the string up until it hits the last numeric value and convert that... just cutting off any dangling none numeric values. For instance " 12zzz" return 12 because it just slices off the zzz
anyways, this works... it's about 20-25 times slower than regular old Double.TryParse... as is expected (I use Regex and the sort). Wonder if anyone has any suggestions to speeding it up. I don't need it to be on par with Double.TryParse or even near it... that'd be ridiculous as I'm expecting it to do a lot more than TryParse even does. But I'd like to try and speed it up just a little bit more.
VB.NET:
Public Shared Function ToDouble(ByVal value As String, Optional ByVal style As System.Globalization.NumberStyles = 111) As Double
If value Is Nothing Then Return 0
Try
Dim m As Match = Regex.Match(value, "^\s+(0x|#|&H)")
If m.Value <> "" Then
value = value.Substring(m.Length)
m = Regex.Match(value, "^[-+]?[0-9a-fA-F]*")
If m.Value <> "" Then
Dim lng As Long
''strip off any style bits that aren't compatible with HexNumber
style = style And System.Globalization.NumberStyles.HexNumber
''make sure we allow hex values
style = style Or System.Globalization.NumberStyles.AllowHexSpecifier
Long.TryParse(m.Value, style, Nothing, lng)
Return CDbl(lng)
End If
Else
m = Regex.Match(value, "^\s+[-+]?[0-9]*\.?[0-9]+")
If m.Value <> "" Then
Dim dbl As Double
Double.TryParse(m.Value, style, Nothing, dbl)
Return dbl
End If
End If
Catch ex As OverflowException
''If it was an OverFlowException, bubble it out
Throw ex
Catch ex As Exception
''if it was any other exception, just return 0
Return 0
End Try
End Function
Last edited: