4
echo off
cls

mode con: cols=55 lines=15

:MAIN
color 07
cls

set /p num1="Specify first number: "
cls
set /p num2="Specify second number: "
cls

set /a num3=%num1%*%num2%

echo %num3%
pause

说 num1 = 30 和 num2= .10 那么它只会显示 0 而不是 .3 那么我如何让它做小数,如果需要,你将如何显示到小数点后 5 位?

4

2 回答 2

3

Batch 不支持浮点运算。您必须依赖外部脚本。

http://www.computing.net/howtos/show/batch-script-floating-point-math/753.html

dos批处理中的浮点除法

于 2013-09-08T23:19:39.320 回答
1
::turn all strings with decimals to integers to perform arithmetic ops
::does not error check for non-numerics or non-numeric formatting
::I check beforehand or it's left as an exercise for the reader
set mynum=12.34
call:str2dec num pos %mynum%
echo str2dec num %num% , pos %pos% for %mynum%
set mynum=12.3
call:str2dec num pos %mynum%
echo str2dec num %num% , pos %pos% for %mynum%
set mynum=12.
call:str2dec num pos %mynum%
echo str2dec num %num% , pos %pos% for %mynum%
set mynum=12
call:str2dec num pos %mynum%
echo str2dec num %num% , pos %pos% for %mynum%
set mynum=.34
call:str2dec num pos %mynum%
echo str2dec num %num% , pos %pos% for %mynum%
GOTO:EOF
:Str2Dec
::Str2Dec(retVal,decPlaces,strIn)
::returns the integer to %1 of strIn, %3, shifted %2 places to the left
::e.g. Str2Dec num pos 12.34 returns num=1234 pos=2
 SET #=%3
 SET lengthInput=0
 SET decpoint=
 SET /A integerVal=0
 SET /A shifted=0
 :lenStr2Dec
 IF DEFINED # (SET #=%#:~1%&SET /A lengthInput+=1&GOTO:lenStr2Dec)
 IF !lengthInput! LSS 2 SET /A integerVal=%3&GOTO:xStr2Dec
 SET numString=%3
 FOR /L %%x IN (1,1,!lengthInput!) DO (
  SET /A startCharIndex=%%x-1
  CALL SET nextChar=%%numString:~!startCharIndex!,1%%
  IF "!nextChar!" == "." (
    SET decpoint=1
  ) ELSE (
    IF DEFINED decpoint SET /A shifted+=1
    SET /A nextNum=!nextChar!
    SET /A integerVal*=10
    SET /A integerVal+=!nextNum!
  )
 )
 :xStr2Dec
 SET "%~1=!integerVal!"
 SET "%~2=!shifted!"
GOTO:EOF
于 2016-01-26T17:02:21.203 回答