System.Convert.ToDouble

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

In VB.NET, when I use System.Convert.ToDouble(string Val) to convert a string
variable to double variable, I got something interesting:

Dim stringVal As String = "101.01"
Dim doubleVal As Double
doubleVal = System.Convert.ToDouble(stringVal)
Msgbox (doubleVal) will return 101.01

but......
Dim stringVal As String = "101.00"
Dim doubleVal As Double
doubleVal = System.Convert.ToDouble(stringVal)
Msgbox (doubleVal) will return 101

where is the ".00" ?
Should I get 101.00 instead of 101?

Did I missed something here?
TIA
 
Why do you think you should have the 00? 101.00=101. And 101 happens to be
what happens when the tostring method is called in this case (which is
called implicitly, since you are not doing it explicitly).

If you want the decimal places to always show up, you should use custom
format strings to ensure that the formatting to your liking - otherwise you
will get default behavior. But I am not sure why you feel there is something
wrong with the default behavior.
 
* Jason said:
In VB.NET, when I use System.Convert.ToDouble(string Val) to convert a string
variable to double variable, I got something interesting:

Dim stringVal As String = "101.01"
Dim doubleVal As Double
doubleVal = System.Convert.ToDouble(stringVal)
Msgbox (doubleVal) will return 101.01

but......
Dim stringVal As String = "101.00"
Dim doubleVal As Double
doubleVal = System.Convert.ToDouble(stringVal)
Msgbox (doubleVal) will return 101

where is the ".00" ?
Should I get 101.00 instead of 101?

The variable is stored in IEEE floating point format after parsing the
string. 'MsgBox' will call 'ToString' on the variable implicitly and
this will convert the variable to a string. You can specify the format
used to format the variable's value as a string in an overloaded version
of the 'ToString' method.

I suggest to turn on 'Option Strict' by placing 'Option Strict On' on
top of your source file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top