Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is CDbl doing?

I had until recently been under the impression that the CDbl(x) operation in VB.NET was essentially a cast (i.e., the VB equivalent of (double)x in C#); but a recent discovery has revealed that this is not the case.

If I have this string:

Dim s As String = "12345.12345-"

And I do this:

Dim d As Double = CDbl(s)

d will be set to the value -12345.12345! Now, don't get me wrong, this is kind of convenient in my particular scenario; but I have to admit I'm confused as to why this works. In particular, I'm confused because:

  • Double.Parse does not work with the above input.
  • Double.TryParse does not work.
  • Convert.ToDouble does not work.

How is CDbl so clever?

like image 530
Dan Tao Avatar asked Apr 16 '10 12:04

Dan Tao


People also ask

What is Cdbl () used for?

CDbl (Function) Converts any expression to a Double. This function accepts any expression convertible to a Double, including strings. A runtime error is generated if expression is Null.

What is Cdbl alert?

CDBL offers FREE SMS Alerts to the Beneficiary Owner (BO) Accountholders mobile phones giving details of daily debit-credit transactions that have taken place in their accounts. To get the SMS Alerts Service please give your mobile number to your DP/ Brokerage house for entering it in your BO Accounts.

Can I open multiple BO account in Bangladesh?

As you are a general investor you need to open a Single or Joint BO account. A person can open two Beneficiary Owners account in his/her in Midway Securities Ltd.: one single account and one joint account.

What is CInt in VB net?

The CInt function converts an expression to type Integer. Note: The value must be a number between -32768 and 32767.


1 Answers

It uses Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(). That function contains a Select statement on the object's GetTypeCode() return value so it can use a custom converter based on the type of the argument. The string converter considers the possibility that the string might contain a currency value and does some processing on the string to deal with that. One allowed format for currency values is a trailing negative sign.

This is not particularly cheap. The quickest way to achieve the same conversion is:

Dim s As String = "12345.12345-"
Dim d As Double = Double.Parse(s, Globalization.NumberStyles.Any)
like image 77
Hans Passant Avatar answered Sep 23 '22 08:09

Hans Passant