# Converting String to Numerical Value

Let's say that in txtbox text1 is a string value "0.062" and in text2 another string value "1.100". If I wish to sum that two values and show the result in text3 that'll be text3.text=text1.text+text2.text. The result will be "0.0621.100" instead of "1.162". But if I do this :
Dim f
Dim ff
ff = 0.062
f = 1.100
ff = ff + f
Text1.Text = ff
The result will be correct(1.162)

How can I declare the string value in txtboxes as a numerical values in vb6?
(dim txt as NumValue
txt=text1.text)???!!!!!
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Private Sub Command1_Click()
Text3.Text = CDbl(Text1.Text) + CDbl(Text2.Text)
End Sub

Text1.Text = ".0005"
Text2.Text = "1.4"
End Sub
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
you have to cast em to the type you wish when you use them ..
0
Commented:

text3.text= val(text1.text) + val(text2.text)

val() will convert string to number.
0
Commented:
don't forget to confirm the data is in fact numeric to begin with...
0
Commented:
dim MyString as string
dim MyInt as Integer
dim MyLong as long
if isnumeric(MyString) then MyInt= cint(MyString)
if isnumeric(MyString) then MyLong = clng(MyString)
0
Commented:
Actually, anaadoul is the closest, but his/her answer will cut off the decimal places.  Try this:

Dim MySum as Long
if isnumeric(text1.text) and is numeric(text2.text) then
MySum = Val(text1.text) + Val(text2.text)
end if

That way you are checking to see if the values in the textboxes are numeric and then you are adding them using the Val function.
0
Commented:
ah the joys of an isnumeric function...
now why cna't every database have one of those... ;-(
0
Commented:
The funny thing is that I inherited some source code from a developer who actually didn't know about the IsNumeric function and ended up writing his own!
0
Commented:
been there seen that done it ....
;-)
0
Author Commented:
Okay I've successfully summed the value in the text1 and text2 using val(string), but now something else troubles me. If I sum that values only once( for example:in text1 is number 2, when I press the command1 then text1.text=val(text1.text)+0.3) the result is ok(2.3), but...
I need to sum the value in text1(now 2.3) with another 0.3 value so the next result would be 2.6, and again another 0.3 is 2.9 and so on... I'm doing this repetitive operation using timer1 like this: timer1's interval is 1 second and every second the timer must add 0.3 to the previous value in text1, ok?      The problem is that after first summing operation (text1+0.3=2.6) the following operations are somehow blocked, but if I change the 0.3 value to some othet value like 1 or 2 or any other non decima number the operation runs normally. Why is this happening and how to correct it?
here's the example(try changing value of pp to non decimal no. and see what's happening):

Dim pp
Dim ss As Boolean
Private Sub Command1_Click()
Text3.Text = "t"

End Sub

Private Sub Command2_Click()
Text3.Text = ""
End Sub

pp = 0.3
End Sub

Private Sub Text3_Change()
If Text3.Text = "t" Then
ss = True

Else
ss = False
End If
End Sub

Private Sub Timer1_Timer()
If ss = True Then
Text1.Text = Val(Text1.Text) + pp
End If
End Sub
0
Author Commented:
It's ok you dont have to answer on previous question but now i've got another problem:

Dim ere
Dim ere2
Dim pp As Double
Dim ss As Boolean
Private Sub Command1_Click()
Text3.Text = "t"
End Sub

Private Sub Command2_Click()
Text3.Text = ""
End Sub

pp = 0.003
ere = Text1.Text
ere2 = CDbl(Text1.Text)
End Sub

Private Sub Text3_Change()
If Text3.Text = "t" Then
ss = True
Else
ss = False
End If
End Sub

Private Sub Timer1_Timer()
d = Text1.Text
If ss = True Then
ere = ere2 + pp
End If
End Sub

WHY WON'T IT COUNT??!!
0
Commented:
Private Sub Timer1_Timer()
d = Text1.Text
If ss = True Then
ere = ere2 + pp
End If
End Sub

You sould use the following instead

pp = 0.003
ere = val(Text1.Text)
ere2 = CDbl(Text1.Text)  //I'm not sure about the difference between val() and CDbl()
End Sub

Private Sub Timer1_Timer()
Dim d as Integer
d = val(Text1.Text)
If ss = True Then
ere = val(ere2) + pp
End If
End Sub

You have to use appropriate data type, when you are trying to declare a variable. Think about the purpose of it. Keep in mind that You should use val() if you want to use text as numeric! With this way you will be safe in the future.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.