Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDbl() function, converts to Double?

Status
Not open for further replies.

NZ

Programmer
Dec 5, 2002
2
US
Hello,

I am using the function CDbl() to convert a String to Double, but this is what happens:

Dim s_num as String;
Dim d_num as double;

s_num = 0.0300;
msgbox(s_num) (shows "0.0300")
d_num = CDbl(s_num)
msgbox(d_num) (shows "300"!!!!)

s_num = 0.03;
msgbox(s_num) (shows "0.03")
d_num = CDbl(s_num)
msgbox(d_num) (shows "3"!!!!)

If I try commas (in case regional configuration has something to do with it, i.e. "0,0300") it's the same.

I'm almost sure, than when I programmed it on another machine, the result was right, AM I WRONG??? IS THIS POSSIBLE???

Please, answer somethimg, even to say that this is not possible or any tips on this.

Thank you.
 
Me again, in the above code, when I do msgbox(d__num), is the same result as msgbox(CStr(d_num)).
 
Dim x As String
Dim y As Double
x = "00.0003"
y = CDbl(x)
MsgBox y

the msgbox y displays 0.003

this is vb not c++ ....dont use semicolons
tryp
 
When assigning a numerical value to a string variable you should take local conventions into account, either by using them in the string represting the value or by using the Cstr function to do the conversion.

Following code shows both alternatives:
Public Sub main()

Dim s_num As String
Dim d_num As Double

s_num = "0,003"
d_num = CDbl(s_num)
MsgBox s_num & vbCrLf & d_num

s_num = CStr(0.003)
d_num = CDbl(s_num)
MsgBox s_num & vbCrLf & d_num

End Sub


Morale: Never ever use s_num = 0.003 unless you want unpredictable results due to VB's coercion techniques.
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top