Batch Script - toInt - GeeksforGeeks

19 Dec, 2021
In the Batch script, every variable is considered as a string and optionally integers. We need numbers all the time especially when it comes to performing system operations. We might extract numbers from a string variable but performing operations on them are not the same and require some additional options. So in this article, we’ll see how to convert a string variable to an Integer and also perform some operations on them for further usage. 
We can use the /A option to convert the string to an integer. A string variable is declared with or without quotes. Anything is more or less a string in the BATCH file unless the character /A is appended before the variable name. 
@echo off
set str=Hello World
set /a num=Hello
echo %str%
echo %num%
It behaves as a pure integer when the /A character is appended to its variable name, all negative and positive integers are supported as the BATCH variables. Even if the variable is declared with a string it is discarded and is set as 0.
Let us suppose we have a string but it is an integer though it was not declared as an integer, we now want the variable to perform some arithmetic. This is where we can use the /A to its true potential to perform arithmetic operations and make the script more flexible and usable when it comes to performing mathematical operations.
To use the /A option for an existing variable we need to reset it to the original value and then perform the desired operation. 
@echo off

set "str=100 this might be the 1st string"
set /A num=str
echo %num%
set /A num=%num% + 10
echo %num%
As seen in the above example, the str variable is originally a string with 100 as one of the initial characters. We can extract the first characters to the integer using the /A operator by creating a new variable num. So we created an integer variable num in after that we can perform all sorts of operations to it. In this case, we added 10 to it. We can also multiply(*), divide(/) or subtract(-) in the BATCH script.
The below script is without the operations to extract the integer from the string as it is. 
@echo off

set "str=100 this might be the 1st string"
set /A num=str
echo %num%
set /A num=%num% 
echo %num%
Now, if you want to directly convert the string into an integer we can do that using a single statement.
@echo off
set "str=100 "
set /A str=(%str% - 10) / 10
echo %str%
In the above example, we converted the string str to an integer and performed some arithmetic to prove that the extracted character was indeed an integer. Hence the operations like subtracting and dividing in the above example can be avoided.
@echo off
set "str=100 "
set /A str=%str%
echo %str%
Note: To convert the string to an integer and perform operations directly the string must only contain a single numeric value and no alphabetic or special characters. You can perform the operations in separate steps if the string variables contain alphabets or other characters than numbers. Thus we were able to convert a string variable into an integer in a BASH script using /an option. 
Batch-script
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
scp command in Linux with Examples
mv command in Linux with examples
Docker - COPY Instruction
SED command in Linux | Set 2
chown command in Linux with Examples
nohup Command in Linux with Examples
Named Pipe or FIFO with example C program
Thread functions in C/C++
uniq Command in LINUX with examples
Start/Stop/Restart Services Using Systemctl in Linux