• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

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
0
Elnazear
Asked:
Elnazear
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Either:

 if IsNumeric(yourstring) then
   .. <yourcode>
 end if

or

Sub ...
dim lngValue as long
on error goto ErrInvalidNumber
lngValue = clng(yourstring)

Exit sub

ErrInvalidNumber:
  <error handling>
Exit Sub

End Sub

CHeers
0
 
crazymanCommented:
i always use

dim lngMyValue as long

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

dim numvalue as integer

numvalue = val("yourstring")

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mdouganCommented:
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

0
 
Anthony PerkinsCommented:
mdougan,

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

Anthony
0
 
mdouganCommented:
<grin>
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now