Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - String to int conversion

Reply
 
Old 09-11-2008   #1 (permalink)
karsagarwal


 
 

String to int conversion

Hi,

I have a string str which can take any odd values from 01 to 99. I
need to convert that to the next positive number.

Therefore 03 becomes 04
25 becomes 26
01 becomes 02

With the code I have below I get 03 to be 4. So I loose the 0 which is
crucial to me. Any suggestions as to how to solve this issue.


str="03"
str=Cint(str) + 1
wscript.echo str


Thanks,
SA

My System SpecsSystem Spec
Old 09-11-2008   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: String to int conversion


<karsagarwal@xxxxxx> wrote in message
news:24d37819-2dae-426c-9d57-f11bf1f06ada@xxxxxx
Quote:

> Hi,
>
> I have a string str which can take any odd values from 01 to 99. I
> need to convert that to the next positive number.
>
> Therefore 03 becomes 04
> 25 becomes 26
> 01 becomes 02
>
> With the code I have below I get 03 to be 4. So I loose the 0 which is
> crucial to me. Any suggestions as to how to solve this issue.
>
>
> str="03"
> str=Cint(str) + 1
> wscript.echo str
>
It must be formated as a string to have the leading zero. One solution
(assuming always 2 digits) would be:

str = "03"
str = Right("0" & CStr(CInt(str) + 1), 2)
Wscript.Echo str

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 09-13-2008   #3 (permalink)
Dr J R Stockton


 
 

Re: String to int conversion

In microsoft.public.scripting.vbscript message <24d37819-2dae-426c-
9d57-f11bf1f06ada@xxxxxx>, Thu, 11 Sep 2008
18:10:37, "karsagarwal@xxxxxx" <karsagarwal@xxxxxx> posted:
Quote:

>
>str="03"
>str=Cint(str) + 1
>wscript.echo str
str="03"
wscript.echo Right(str+101, 2)


ASIDE - it appears that one cannot (one can in JavaScript) apply unary
plus and convert String to Number - document.write +"03" gives '03'.
But one can apply unary minus and convert - document.write -"03" gives
'-3' and one can apply it twice - document.write --"03" gives '3' .

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
My System SpecsSystem Spec
Old 09-13-2008   #4 (permalink)
Old Pedant


 
 

Re: String to int conversion



"Dr J R Stockton" wrote:
Quote:

> str="03"
> wscript.echo Right(str+101, 2)
My head hurts.

In so many other uses of string+number, I have seen that the result is
string concatenation. Why this simple usage works is a mystery to me.

Oh, I understand what happens, under the covers. VBScript attempts to
coerce the two values being "added" to the same data type, using
VariantChangeTypeEx. But it has been my experience *many* times in the past
that the preferred conversion is integer to string (so that then the +
operator causes concatenation). Wish I had a handy example; if I find one
I'll repost. But anyway, clearly in a simple situation such as this (one
string, one integer) it works exactly as you would want it to.

I wonder if the precendence (string vs. number) has changed between VBS 3
and VBS 5? Because when I was working in the source code of VBS 3 (about
1999), I pretty clearly remember it the other way around. Oh, well. Maybe
not. Maybe senility has set in.





My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
conversion from .net String to mfc CString .NET General
problems with $var | select-string -pattern $string -q PowerShell
bug in string<->datetime conversion? PowerShell
How export-csv deals with string versus string[] PowerShell
String PRODUCT_NAME was not found in string table Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46