문제

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"

도움이 되었습니까?

해결책 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.

다른 팁

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%
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top