Thanks:  0
Likes:  0
Dislikes:  0

# Thread: How to convert string to number

1. ## How to convert string to number

Hi,

I am retreiving cell value in a grid.
and storing in a variable.
Example:
a="123.100"
b="100.009"
c="223.109"

I want to compare c,a+b.

Thanks
Pramod

2. ## Re: How to convert string to number

VERY interesting question.
Looks like simple. But it's NOT.

Basicaly, you can easy convert String2Double or integer or any Numeric value by multiplying value
(As I remember, It's Dima's solution, posted to this group)
Like that:
MsgBox (a*1) + (b*1)
MsgBox (c*1)

But, with comparison operation - those two values never be equal, because of floating point. Meaning, even if it looks like equal - it's NOT for computer's calculation.
So,
((a*1) + (b*1)) is NEVER-EVER equal (c*1)
[Just try it for interest in any combination
with If statement :-}]

Can be managed with low level language, but not interpreter, like any script.
Work around would be: specify precesion for this calculation.

a="123.100"
b="100.009"
c="223.109"
If (((a*1) + (b*1)) - (c*1)) &lt; 0.000001 Then
MsgBox "Equal"
End If

Regards,
Janna

3. ## Re: How to convert string to number

a="123.100"
b="100.009"
c="223.109"

d=cdbl(a) + cdbl(b) - cdbl(C)
d=d*1000
d=cint(d)
If d=0 Then
msgbox "Equal"
Else
msgbox "Not equal"
End If

Thank you.

5. ## Re: How to convert string to number

Nice solution, Tarun!
Exellent.
I like it!!!
One more from me:
I did as you did - separated calculation.
But not everything - only SUM.
And then convert result back to String.
After that - nothing prevents (no floatingPoint, no precision) comparison. :-}

a="123.100"
b="100.009"
c="223.109"

ab=(a*1)+(b*1)

If CStr(ab) = c Then
MsgBox "Equal"
End if

6. ## Re: How to convert string to number

Thank you Tarun and Janna

7. ## Re: How to convert string to number

Hi tarun,
why to do all those stuffs..i feel the following as a simple solution..
a="123.100"
b="100.009"
c="223.109"

d=cint(c)-(cint(a)+cint(b))
If d=0 Then
msgbox "Equal"
Else
msgbox "Not equal"
End If
this works for me

8. ## Re: How to convert string to number

int will actually not a exact comparison thats why i have used double. But the problem with double is that subtraction of two equal variable some time lead to a very small value something like 2.44646E-14. Now we know that we were checking 3 digits after decimal thats why is multiplied the d by 1000 and not 100.

I hope this clarifies your doubt

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.