468,766 Members | 1,300 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

System.Convert.ToDouble


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
Nov 21 '05 #1
2 6147
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" <ja***@hotmail.com> wrote in message
news:t9********************************@4ax.com...

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

Nov 21 '05 #2
* Jason <ja***@hotmail.com> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Irepan | last post: by
10 posts views Thread by | last post: by
3 posts views Thread by user | last post: by
2 posts views Thread by ORC | last post: by
3 posts views Thread by mrajanikrishna | last post: by
2 posts views Thread by trondhuso | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.