2017-05-24 117 views
0

试图创建一个可以接受小数输入的批处理程序(我没有其他选项,只能使用批处理),输入仅限于小数点后两位。我的问题是我怎样才能完善输出以提高准确性。例如,用户输入是100.77 * 0.80,答案是80.616,我只能输出80.61而不是80.616,我想输出80.62。而我更大的问题是,小数答案将被用来从原来的数量中减去,这将导致小数级的数学错误。答案将是80.61和20.16。 这是我的计划:批处理程序中的小数位数的精度

@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" 

:getFee 
set /P "Amt=Enter the Amount (100.00): " 
set /P "Disc=Enter the Discount Percentage: " 
if "!Amt:~-%decimalsP1%,1!" equ "." goto AmtDeci 

:getNumber 
goto NoDeci 

:AmtDeci 
set "fpA=%Amt:.=%" 
set "fpB=%Disc:.=%" 
set /A mul=fpA*fpB/one 
set discout=!mul:~0,-2!.!mul:~-2! 
echo The Discount is: %discout% 
set /A "fpD=%discout:.=%" 
set /A sub=fpA-fpD 
set Amtout=!sub:~0,-%decimals%!.!sub:~-%decimals%! 
echo The Amount less discount is: %Amtout% 
pause 
Exit /B 
:NoDeci 
set /a discout=%Amt%*%Disc%/100 
set /a Amtout=%Amt%-discout 
echo The Amount less Discount is: %Amtout% 
echo The Discount is: %discout% 
pause 
+1

批次只能用32位整数进行数学运算。我猜这个要求实际上是你不能在你的电脑上安装任何东西;改用PowerShell或VBScript。 – SomethingDark

回答

2

我假设你正在使用的this answer描述的方法。你总是应该包含一个到你的代码的原始源的链接。

你想要做什么是很简单:通过one 0.5相当于只加分前的相乘的结果:

set /A mul=(fpA*fpB+50)/one 

输出例如:

Enter the Amount (100.00): 100.77 
Enter the Discount Percentage: 80 
The Discount is: 80.62 
The Amount less discount is: 20.15 
+0

谢谢!是的,我做了,因为我收集了很多东西,忘记了它们的位置,所以我忘了把它链接起来,这是我第一次问这里,对不起!谢谢! –