Batch Script - how to solve arithmetic error

Viewed 39

I'm very new to batch script.

I'm getting this error and clueless how to solve. im supposed to get amount (ml) =9.375, but instead getting 5.

Im trying to come up with dose calculator

The whole idea is getting for right amount, weight is 15kg, dose is 15mg/kg. strenght of solution is 120mg/5ml.

So the amount is ((15*15)/120)*5=9.375ml

Where I might go wrong here?

@echo off
Title Syrup dose calculator
color 5a

set /p weight= Enter child's weight:
set /p dose= Enter required dose in mg/kg:
set /a totaldose= %weight%*%dose%
set /a amountml = ((%weight%*%dose%)/120)*5)
echo.
echo Total(mg) :%totaldose%
echo Amount(ml) : %amountml%

echo.
pause

goto loop
1 Answers

There is no floating-point arithmetic in batch. set /a (which is the only thing in cmd that does arithmetic) can only handle Integers (to be exact: INT32). You can either write some messy code to do it "by hand" or use the help of another language (for example PowerShell) and get it's output with a for /f loop.

(Code simplified for demonstration)

@echo off
setlocal 

set weight=15
set dose=15
set /a totaldose=%weight%*%dose%
for /f "delims=" %%a in ('powershell %weight%*%dose%/120*5') do set amountml=%%a
echo Total(mg) : %totaldose%
echo Amount(ml) : %amountml%
Related