Home > Mobile >  How to use batch file to do a math problem involving a percentage and a adding a decimal number afte
How to use batch file to do a math problem involving a percentage and a adding a decimal number afte

Time:01-18

I have done a lot of research and it seems that multiplying by a % percentage doesn't seem possible in a BAT file ?

What I am trying to accomplish is asking a user for an input number ( lets call it A ) for example

Then multiplying that number by a set percentage number and then adding something like .41 to the total

so the equation in simple math would be a*xx% .41= ???

but I have not found a way to do this in a BAT file ...

Anyone ? Any ideas ? thank you for your time and assistance with solving this ?

I tried looking for the answers in google etc etc and doing it as simple math in BAT but it does not work :(

UPDATE ( ****** )

ok so I have the basics down after some further research but what I would like to do now is be able to save the variables to a text file test to see if the file exists if it does ask if the current values are correct and if no then give option to change them ( same if file doesn't exist )

here is what I have so far

@echo off

set /P x="Percentage:"
set /P y="Price:"
set /P Z="Fixed Rate:"

cscript //nologo calculate.vbs %x% %y% %z% > results.txt

set /P charge= < results.txt 
pause

del results.txt  

echo charge Fee Total: %charge%

cscript //nologo calculate2.vbs %y% %charge% > results2.txt

set /P total= < results2.txt 
pause

del results2.txt

echo sale total amount: %total%

pause

CodePudding user response:

From what I understand in your comments, your calculation should be ((%x% / 100^)*%y% %Z%, if not, feel free to change them:

@echo off

if not exist "calculations.txt" call :setVars

if exist "calculations.txt" for /f "usebackq delims=" %%i in ("calculations.txt") do set "%%i"
echo fixed rate = %Z%
echo Percentage = %x%
choice /c CR /M "Continue or re-set?"
if errorlevel 2 call :setVars
for /f "usebackq delims=" %%i in ("calculations.txt") do set "%%i"
set /P "y=Price: "


for /F "delims=" %%i in ('powershell ((%x% / 100^)*%y%   %z%^)') do echo %%i

goto :EOF
:setVars
set /P "x=Percentage: "
set /P "Z=Fixed Rate: "x
(echo x=%x%
 echo z=%z%
)>"calculations.txt"

The initial run will detect that calculations.txt does not exist, and request the values. It will then pass them to the file, the next run, the file is found, and it will only request price, not fixed rate or percentage.

Additionally, to see what is in calculations.txt simply add the line echo %x% %z% before the line goto :EOF

  • Related