0

Trying to create a batch program (I have no other option but only use batch) that can accept a decimal input, the input is only specific up to two decimal places. My problem is how I can round-off the output to improve accuracy. For example the user input is 100.77*0.80, the answer is 80.616, I can only output 80.61 and not 80.616, I want an output of 80.62. And my bigger problem is that the decimal answer will be used to subtract from the original amount which will cause a Mathematical mistake in the decimal level. The answer will be 80.61 and 20.16. Here's my program:

@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
    Batch can only do math with 32-bit integers. I'm guessing the requirement is actually that you can't install anything on your computer; use PowerShell or VBScript instead. – SomethingDark May 24 '17 at 23:22

1 Answers1

2

I assume you are using the method described at this answer. You always should include a link to the original source of your code.

What you want to do is very simple: just add the equivalent of 0.5 to the result of the multiplication before divide it by one:

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

Output example:

Enter the Amount (100.00): 100.77
Enter the Discount Percentage: 80
The Discount is: 80.62
The Amount less discount is: 20.15
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thank you! Yes I did, I forgot to link that in since I collected a lot of stuff and forgot where I got them and it's my first time asking here, I'm sorry! Thanks! – David Bifrons May 25 '17 at 15:49