2017-05-24 78 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

謝謝!是的,我做了,因爲我收集了很多東西,忘記了它們的位置,所以我忘了把它鏈接起來,這是我第一次問這裏,對不起!謝謝! –