[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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