Is there an efficient method in VB to check if a string can be converted to a double?
I'm currently doing this by trying to convert the string to a double and then seeing if it throws an exception. But this seems to be slowing down my application.
Try ' if number then format it. current = CDbl(x) current = Math.Round(current, d) Return current Catch ex As System.InvalidCastException ' item is not a number, do not format... leave as a string Return x End Try
To do this, use the ToString(IFormatProvider) and Parse(String, IFormatProvider) methods of that value's type. For example, use Double. Parse when converting a string to a Double , and use Double. ToString when converting a value of type Double to a string.
The CDbl function converts an expression to type Double. The expression must be a numeric value.
We can convert String to Double object through it's constructor too. Also if we want double primitive type, then we can use doubleValue() method on it. Note that this constructor has been deprecated in Java 9, preferred approach is to use parseDouble() or valueOf() methods.
The CDate function converts a valid date and time expression to type Date, and returns the result. Tip: Use the IsDate function to determine if date can be converted to a date or time.
Try looking at Double.TryParse() if you are using .NET 1.1/2.0/3.0/3.5/4.0/4.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With