I have batch file and When user Writes number,Then automatically Adds numbers together. But if the User Writes Decimal number,Cmd Can not calculate.
Any idea?
Thanks in advance.
I have batch file and When user Writes number,Then automatically Adds numbers together. But if the User Writes Decimal number,Cmd Can not calculate.
Any idea?
Thanks in advance.
Here is batch/vbscript hybrid.
The batch passes the arguments and parses the result and stores it in the variable Result
::'@echo off &for /f "delims=" %%a in ('cscript //nologo //e:vbscript "%~f0" "%*"') do set "Result=%%a"
::'echo(Result=%Result%&exit /b
Wscript.echo Eval(Wscript.Arguments(0))
wscript.quit
There is one invisible char following the ::'
(I hope they get transferred)
Some examples where the batch file is stored as Math.cmd
(my locale setting has a decimal comma)
>Math 3.76 + 2.54
Result=6,3
>Math 3.76 - 2.54
Result=1,22
>Math 3.76 * 2.54
Result=9,5504
>Math 3.76 / 2.54
Result=1,48031496062992
Pure batch only supports 32bit integers, you therefore have to make some changes to make it work which is not difficult, but is is a slightly larger script. Instead use a combination of vbscript
and batch
.
Create a file and name it something like MyCalc.cmd
and paste the below into it:
@echo off
setlocal
>"%temp%\calculate.vbs" echo Set clc = CreateObject("Scripting.FileSystemObject") : Wscript.echo (%*)
for /f "delims=" %%a in ('cscript /nologo "%temp%\calculate.vbs"') do set "var=%%a"
echo %var%
del "%temp%\calculate.vbs"
To use it, open cmd.exe
cd to where the file you saved is, then run MyCalc.cmd 10*0.5
or MyCalc.cmd 8.2/0.5
or MyCalc.cmd 3.2-0.3
or MyCalc.cmd 10+9.5
etc.
Alternatively, add the path to the script to the system's PATH
environmental and you do not have to cd
to it, or just call it straight from cmdline as d:\path to file\scripts\MyBat.cmd 10+2.3
etc.
This solution is one provided from an earlier post here
@echo off
setlocal EnableDelayedExpansion
set decimals=2
set /A one=1, decimalsP1=decimals+1
for /L %%i in (1,1,%decimals%) do set "one=!one!0"
:getNumber
set /P "numA=Enter a number with %decimals% decimals: "
if "!numA:~-%decimalsP1%,1!" equ "." goto numOK
echo The number must have a point and %decimals% decimals
goto getNumber
:numOK
set numB=2.54
set "fpA=%numA:.=%"
set "fpB=%numB:.=%"
set /A add=fpA+fpB, sub=fpA-fpB, mul=fpA*fpB/one, div=fpA*one/fpB
echo %numA% + %numB% = !add:~0,-%decimals%!.!add:~-%decimals%!
echo %numA% - %numB% = !sub:~0,-%decimals%!.!sub:~-%decimals%!
echo %numA% * %numB% = !mul:~0,-%decimals%!.!mul:~-%decimals%!
echo %numA% / %numB% = !div:~0,-%decimals%!.!div:~-%decimals%!
for example:
Enter a number with 2 decimals: 3.76
3.76 + 2.54 = 6.30
3.76 - 2.54 = 1.22
3.76 * 2.54 = 9.55
3.76 / 2.54 = 1.48
Try This :
@echo off
set /p x=PLS Enter Your decision =
echo WSH.Echo Eval(Wscript.Arguments(0))>>Q.vbs
cscript //nologo Q.vbs %x%
Del "Q.vbs"
Pause>nul