如何在批处理文件中显示余数?

时间:2014-09-24 20:15:25

标签: batch-file

我使用的是Windows 7,我有以下内容:

set /p param=Please type a number
set /a answer= %param%/2
echo Your answer is %answer%

所以他们输入的任何数字除以2.然而,如果它是一个奇数,它只是将它向下舍入到整数。例如,如果我输入7,它会给我6因为7/2是3.5对吗?但它只是将其舍入到整数而不是留下小数或在这种情况下为3.5。有没有办法让它显示出来?感谢

2 个答案:

答案 0 :(得分:0)

set /p param=Please type a number
set /a answer= %param%/2
set /a mod=%param%%%2
echo Your answer is %answer%.%mod%

编辑:

set /p param=Please type a number
set /a answer= %param%/2
set /a mod=%param%%%2
if %mod% equ 1 set mod=5
echo Your answer is %answer%.%mod%

但这只会除以2。

对于实时浮点运算,您可以通过命令行和jscript函数eval使用MSHTA:

@echo off
setlocal

set /p exp=expression to calculate: 


:: Define simple macros to support JavaScript within batch
set "beginJS=mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(eval("
set "endJS=)));""


:: FOR /F does not need pipe
for /f %%N in (
  '%beginJS% %exp% %endJS%'
) do set result=%%N

echo result=%result%

答案 1 :(得分:0)

没有办法使这项工作正常,因为技术上不支持使用浮动数学。

你可能想要使用powershell。这会容易得多。

相关问题