Question

I am currently working in batch. I want to know a way to multiply or add with percents and/or decimals.

Example:

set /a wcexpt= %wcexpt% * ??

Every time I try this:

set /a wcexpt= %wcexpt% * .005

it results in a 0.

When I try this:

set /a wcexpt= %wcexpt% * %5

it results in "missing operand"

Was it helpful?

Solution 2

You may simulate operations with decimals using only integer numbers in a very simple way. For example:

@echo off

set /P "wcexpt=Enter value: "

rem Get 5% of wcexpt:
set /a percent=wcexpt * 5

rem Show result:
echo The 5%% of %wcexpt% is %percent:~0,-2%.%percent:~-2%

Output:

C:\> test
Enter value: 1234
The 5% of 1234 is 61.70

Of course, a much more detailed simulation that include all arithmetic operations is possible. See this post for further details.

OTHER TIPS

The short answer is that batch uses integers only, so you must convert your formulae to use integers.

Also, the integers are limited to ~ +/- 2**31.

You can extend the range - but it takes some mathematical gymnastics and will be extremely slow.

I tried this and it is working it is giving the result as 1

set /a wcexpt = 20
set /a wcexpt= (wcexpt * 5)/100
echo %wcexpt%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top