Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Double - VB

Tags:

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 
like image 571
mscccc Avatar asked Jul 23 '09 14:07

mscccc


People also ask

How do I convert a string to a double in Visual Basic?

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.

What is CDbl in Visual Basic?

The CDbl function converts an expression to type Double. The expression must be a numeric value.

Can you convert string to double?

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.

What is CDate in VB net?

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.


1 Answers

Try looking at Double.TryParse() if you are using .NET 1.1/2.0/3.0/3.5/4.0/4.5

like image 82
Kane Avatar answered Sep 25 '22 22:09

Kane