CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 1999
    Posts
    492

    VBScript: Converting string to a number

    I have a number in string format that my VBScript won't treat as a number.

    For example:


    myVar = "100"
    If mVar >= 100 then
    :
    :




    I get an error on the "If" line. I program in C++ all the time so I completely understand the difference between "100" and 100, but what I don't understand is when this bizarre VBScript and its VARIANT datatype will do this or that for me.

    So, my simple question is how do I convert this into a number? I checked the MSDN and I swear that Microsoft couldn't design a more difficult documentation package than the MSDN if they tried!

    Why are the "tolerant" so easy to offend?

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: VBScript: Converting string to a number

    Try:

    Somevar = "100"

    if CInt(somevar) <= 100 then
    ....





  3. #3
    Join Date
    Aug 1999
    Posts
    492

    Re: VBScript: Converting string to a number

    Doesn't work. I get:


    Microsoft VBScript runtime error '800a000d'

    Type mismatch: 'CInt'

    /myfile.asp, line 29


    Why are the "tolerant" so easy to offend?

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: VBScript: Converting string to a number

    I tried the following code and it works fine

    dim a
    a= "100"
    if a>90 then msgbox "greater"

    check theat you have the write version of Internet Explorer. I believe it must work with IE 5 +

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    Aug 1999
    Posts
    492

    Re: VBScript: Converting string to a number

    I think the server might still be a IE 4 system. What is the solution if I have such an old server?

    Why are the "tolerant" so easy to offend?

  6. #6
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: VBScript: Converting string to a number

    First solution is to upgrade (it is free)
    Second - in VBS all the variables are variant. There are no strings or numbers. Try to enter you var
    instead a ="100"
    try a = 100

    I am not sure, but it might help


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  7. #7
    Join Date
    Oct 2001
    Location
    Singapore
    Posts
    8

    Re: VBScript: Converting string to a number

    Try with this


    <%


    dim myString

    myString = "100"

    if Cint(myString) <= 100 then
    ...........
    else
    ...........
    end if

    %>

    its working, try this also CLng,CDbl,CSng

    Shekhar




  8. #8
    Join Date
    Aug 1999
    Posts
    492

    Re: VBScript: Converting string to a number

    Thanks for the replay, but for some reason I get scripting errors with the function CInt. From someone else's comment, I think my problem has to do with using an old 4.X version of Internet Explorer.



    Why are the "tolerant" so easy to offend?

  9. #9
    Join Date
    Jul 2017
    Posts
    1

    Re: VBScript: Converting string to a number

    You may use

    dim myString

    myString=""

    if Cint(myString) < 100 then
    ...........
    else
    ...........
    end if

    then you get scripting errors with the function CInt.

    now try this one

    if Cint( "0"&myString) < 100 then
    ...........
    else
    ...........
    end if


    This works. I tested it.
    Rara...

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VBScript: Converting string to a number

    I would think after 16 years the problem has either been solved or forgotten
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured