Thread: Function for Number vs String

1. Function for Number vs String

How can I differentiate these 2 values ?

The following "IsNumeric" function returns True in both case.
These are the values I get from the database and I want to change only the numeric value to Fix. What function understands these 2 values are different?

Thanks.

x=457.523
y="457.523"

MyCheck = IsNumeric(x) ' Returns True.

MyCheck = IsNumeric(y) ' Also Returns True.

2. Re: Function for Number vs String

Try the TypeName function. Based on the result of that function, you decide which variable to fix.

3. Re: Function for Number vs String

I prefer VarType:

whatVarType= "123.45"
res =VarType(whatVarType)

If res = VBString Then
MsgBox "Its a String"
End If

whatVarType= 123.45
res =VarType(whatVarType)

If res = VBString Then
MsgBox "Its a String"
End If

Mark Smith.

4. Re: Function for Number vs String

For some reason I am getting :
a= 234.222( decimal subtype value)
res = varType(a) returns 14 (i.e. Decimal subtype )

How do I convert decimal subtype to Fix?

b=Fix(a) didn't work in my case.

5. Re: Function for Number vs String

I got it working by converting Double subtype to CDbl and then to Fix.

Thanks all.

6. Re: Function for Number vs String

I'm not sure how that happened. I copied and pasted your example into QTP as follows:

a= 234.222
res = varType(a)
msgbox res
b=Fix(a)
msgbox b

The msgboxes result the in Type 5 (Double-precision floating-point number) and b=234.

a= "234.222"
res = varType(a)
msgbox res
b=Fix(a)
msgbox b

The msgboxes result the in Type 8 (String) and b=234. I never got a Decimal type, and both times the Fix() function did just what it should do. Then I did it again for your initial value (457.523):

a= 457.523
res = varType(a)
msgbox res
b=Fix(a)
msgbox b

a= "457.523"
res = varType(a)
msgbox res
b=Fix(a)
msgbox b

And I got the same results (type 5 and type 8). Based on that, I would write it something like this:

a = your SQL call to the database
res = varType(a)
If res = 5 Then
a=Fix(a)
End If

Which is pretty much what the two Marks were saying.

There may be more than one numerical datatype in your database (Types 4, 5, and 6 come to mind) so you may need to compound the If..Then statement to check for all possible types to which you might apply the Fix() function.

If all that fails, you might reverse-engineer the fix() function into its component functionality and rewrite it in your script.

So instead of b=Fix(a) you could try b = Sgn(a) * Int(Abs(cDbl(a))). When I put this into my tests (above) I got the same values.

And if not that, then I'm stumped.

Blake

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.