Solved

convert string to number

Posted on 2002-04-25
6
513 Views
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
0
Comment
Question by:Elnazear
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6968065
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
 
LVL 13

Expert Comment

by:crazyman
ID: 6968110
i always use

dim lngMyValue as long

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

Expert Comment

by:n_narayanan
ID: 6968336
You can also simply use val function,

dim numvalue as integer

numvalue = val("yourstring")

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 18

Expert Comment

by:mdougan
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

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6968697
mdougan,

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

Anthony
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6970031
<grin>
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now