WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

FAQs - Frequently Asked Questions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Converting Strings into Numbers and Negative Typing

Keywords:      string convert negative typing typeless automagically

Question:

I wonder if WinBatch has a function which converts strings into numbers : equvalent of atoi() in C ? I thought if it has IsNumber() function it should have conversion too however I could not find it.

Answer:

WinBatch has "negative typing". All conversion is automagic.
	a= 5 + "27"
	b= strcat("Sue is ", 7 , " years old")
	Message(a,b)

Question:

Why does the following always evaluate to true?
x="abcd--"

if x>1000
  Message("Hello", "World")
endif

Answer:

This is WinBatch's "negative typing" as opposed to "strong typing" in some other languages.

WinBatch looks at the type of the x variable, decides that it is NOT a number, looks at the 1000, decides it is a number, but not compatible with a compare with x, so it converts the 1000 to a string and the real compare is if "abcd--" > "1000".

Then it does a string compare. "a" comes after "1" in the ansi collating sequence, so "abcd--" is indeed greater than "1000".


Question:

Has anyone noticed that initialization of variables has changed somewhat in version 2000 and later versions of WB?

This code worked in version 99p:

x=" "
while 1
x=x+1
if x==10 then break
endwhile
exit
but not now. Now you get error "'x=x+1' 3057: Variable could not be converted to a valid number".

Is this a bug, or will variables need to be initialized to zero?

Answer:

Interesting. There has been some bug fixes in "number management" and "negative-typing" that you may have run into.

WinBatch tries *real hard* to convert strings to numbers for processing. But it has to be carefull not to get carried away otherwise stuff like

if 0 == " "
would be true. I think a bug fix in this area may have bit you.

Yes. Numbers should be initialized to numbers not nulls strings or blanks.

You can do stuff like

a = "5" + "3"
which is adding the strings 5 and 3. Winbatch recognizes that you want to add two things, but they are strings. So it sees if it can convert these strings to numbers. If the conversion is successful, it adds the numbers.

So what is a number. One must specify rules that describe the allowed numbers.

so

2 is a number
2.0 is a number
2. is a number
0.2 is a number
.2 is a number
. well it was a number for a while (0.0) but we fixed it.

2e10 is a number
1e1 is a number
e is not a number (it was for a while) 1e0
" " was a number in some cases (0) until we decided to insist that to be a number the string actually has to have a number in it.

Many language have "typing" also called "strong typing" where you have to very specifically state what each variable is and any attempt to use it for another purpose gets some kind of error.

WinBatch sports "negative-typing" where it will try all kinds of tricks to attempt to coerce the provided item into the kinds it actually needs for the computation

a=strcat("5" + "4", " Hmmm")
Converting numbers to strings is usually not a problem until you get to the wierd floating point numbers that can only be represented in E notation
-1.2345E-123
Which looks confusing in a string. However converting strings to numbers can be tricky
1A1 is not a number
1E1 is
under the theory that if you need a number and the string fits the category, then lets do it.

Effects:

"1E1" == "010"

basically 10 == 10
But if you are comparing part numbers to see if part number 1E1 is the same as part number 010 you will get wierd results. That's life. There are workarounds.

So "negative-typing" is handy but also dangerous. That's why some language has "strong-typing" to make you define all your variables and what they can do before they even look at your code. Actually it is essential for bigger projects. WinBatch on the other hand was designed as a quick and dirty development language for shorter programs where one person could actually understand all the code involved. Negative typing is very very handy for small programs.


Article ID:   W12985
Filename:   Converting Strings to Numbers and Negative Typing.txt
File Created: 2001:04:03:14:57:30
Last Updated: 2001:04:03:14:57:30