问题
How do I use the SET command in windows for floating point arithmatic. /A stands for arithmetic and %VAR% prints the data of VAR not the name.
For example when I do:
SET /A VAR="2.5+3.1"
ECHO %VAR%
pause
I get the error: 'Missing Operator'. The output (5.6) should be converted to floating point too
I'm still busy learning the basic syntax.
Regards, Snipe
回答1:
The arithmetic operations of SET /A command are performed over 32-bits integer numbers only; however, you may easily simulate fixed point operations with SET /A if you choose a number of decimal digits and preserve they throughout the operations. For example:
REM Select two decimal digits for all operations
SET /A VAR=250+310
ECHO RESULT: %VAR:~0,-2%.%VAR:~-2%
This method allows you to directly perform addition or subtraction of two "Fixed Point" numbers. Of course, if you want to input numbers in the usual way (with decimal point), you must eliminate the decimal point and left zeros before perform the operations.
You may also directly multiply or divide a FP (fixed point) number by an integer and the result is correct. To multiply or divide two FP numbers we may use an auxiliary variable called ONE with the correct number of decimal places. For example:
rem Set "ONE" variable with two decimal places:
SET ONE=100
rem To multiply two FP numbers, divide the result by ONE:
SET /A MUL=A*B/ONE
rem To divide two FP numbers, multiply the first by ONE:
SET /A DIV=A*ONE/B
Further details at this post.
回答2:
you cannot do this with batch.It works only with integers. But you can use Jscript or powershell:
>powershell 5.6+3.1
8.7
For jscript you'll need to create aditional bat and call it (e.g. jscalc.bat):
@if (@x)==(@y) @end /***** jscript comment ******
@echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b 0
@if (@x)==(@y) @end ****** end comment *********/
WScript.Echo(eval(WScript.Arguments.Item(0)));
Example:
>jscalc.bat "4.1+4.3"
8.4
have on mind that the jscript above is not pretty robust and will not handle bad formated expression.
To set the result into variable in both cases you'll need an wrapping FOR /F
:
>for /f "delims=" %# in ('powershell 5.6+3.1') do set result=%#
>set result=8.7
>for /f "delims=" %# in ('jscalc 5.6+3.1') do set result=%#
>set result=8.7
Most probably you have installed powershell , but it's not available on all machines (by default is not installed on Vista,XP,2003) so if you don't have you'll need the jscalc.bat
And also you can use jscript.net (the ANOTHER jscript).Creating a .bat file that uses this is a little bit more verbose and creates a small .exe file but is an option too .Here's the jsnetcalc.bat
:
@if (@X)==(@Y) @end /****** silent jscript comment ******
@echo off
::::::::::::::::::::::::::::::::::::
::: compile the script ::::
::::::::::::::::::::::::::::::::::::
setlocal
if exist "%~n0.exe" goto :skip_compilation
:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
if exist "%%v\jsc.exe" (
rem :: the javascript.net compiler
set "jsc=%%~dpsnfxv\jsc.exe"
goto :break_loop
)
)
echo jsc.exe not found && exit /b 0
:break_loop
call %jsc% /nologo /out:"%~n0.exe" "%~f0"
::::::::::::::::::::::::::::::::::::
::: end of compilation ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation
::
::::::::::
"%~n0.exe" %*
::::::::
::
endlocal
exit /b 0
****** end of jscript comment ******/
import System;
var arguments:String[] = Environment.GetCommandLineArgs();
Console.WriteLine( eval(arguments[1]) );
Example:
>jsnetcalc.bat 4.5+7.8
12.3
And even one more way that uses MSHTA (in expression set the expression you want to calculate):
@echo off
setlocal
:: Define simple macros to support JavaScript within batch
set "beginJS=mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(eval("
set "endJS=)));""
set "expression=8.5+3.5"
:: FOR /F does not need pipe
for /f %%N in (
'%beginJS% %expression% %endJS%'
) do set result=%%N
echo result=%result%
Can be edited to accept arguments like the examples above also...
来源:https://stackoverflow.com/questions/25951196/set-command-floating-point-numbers