Link to home
Start Free TrialLog in
Avatar of Elnazear
Elnazear

asked on

convert string to number

in vbscript
how can I convert string to Number
and if the string is invalid number format how can check that with before the program breaked
with examples
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i always use

dim lngMyValue as long

lngMyValue = clng(val("yourstring"))
lngMyValue will be 0 if the string is invalid
You can also simply use val function,

dim numvalue as integer

numvalue = val("yourstring")

That's a trick question.  In VBScript, there are no number datatypes, just variants (ok, you can have a variant that is a type variant/integer or something like that). You can't dim something as long or integer in VB script.

As suggested above, if you need to pass a string as a particular datatype, say to an external vb component or something, then you can use the conversion functions like
CLng(), CInt() etc.

I'd suggest never using the Val statement as suggested.  Val has the following quirks:

Val("555-1212") gives you 555

Val("59.95") gives you 59

Val("1,000,000") gives you 1

mdougan,

There is no Val function in VBScript. Probably because it does not support regional locales.

Anthony
<grin>