convert string to number

Posted on 2002-04-25
Last Modified: 2012-05-04
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
Question by:Elnazear
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 50 total points
ID: 6968065

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


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

Exit sub

  <error handling>
Exit Sub

End Sub

LVL 13

Expert Comment

ID: 6968110
i always use

dim lngMyValue as long

lngMyValue = clng(val("yourstring"))
lngMyValue will be 0 if the string is invalid

Expert Comment

ID: 6968336
You can also simply use val function,

dim numvalue as integer

numvalue = val("yourstring")

Independent Software Vendors: 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!

LVL 18

Expert Comment

ID: 6968575
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

LVL 75

Expert Comment

by:Anthony Perkins
ID: 6968697

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

LVL 18

Expert Comment

ID: 6970031

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question