Any reason this won't calculate correctly?

April 18, 2011 at 02:14:35
Specs: Windows 7
I'm trying to make a calculator to work out how much VAT has been added to an item before it got to it's total price. The code is shown below:

:ADDEX
CLS
SET /P MNP=How many items would you like to add? 
SET /A MN=0
SET /A IT1=0
IF %MNP% GTR 0 GOTO ADDUP
GOTO ADDEX

:ADDUP
SET /P IT=What is the price of this item? (GBP) 
SET /P ITE=What is the Exempt amount of this item? (Pct) 
SET /A IT1=%IT1% + (%IT% * (%ITE% / 100))
SET /A MN=%MN% + (%IT% - %IT1%)
SET /A MNP=%MNP% - 1
IF %MNP% GTR 0 GOTO ADDUP
ECHO %MNP%  %MN%
PAUSE
GOTO Menu

Can you see any reason why this may not work? If I enter 100 and then 20, it should return 80. If I do it twice, it should return 160 - but it returns 200.


See More: Any reason this wont calculate correctly?

Report •

#1
April 18, 2011 at 02:50:42
The script doesn't work because batch math can handle natively integers only, i.e. (%ITE% / 100) for a value of ITE = 20 is 0 while (%ITE% %% 100) is 20.

You can handle real numbers and even floating point computations but by your own effort and that may require a huge work.


Report •

#2
April 18, 2011 at 03:08:01
Thank you for your reply, is there any way you think I can get around this?

*UPDATE*

I am one step further, it now returns 80 if I do 1 calculation, but when I do 2 - it returns 140 (Both being 100 and 20)

SET /A IT1=%IT1% + %IT% * %ITE% / 100

That's the change I made (Removed brackets) - any advice? Thank you.


Report •

#3
April 18, 2011 at 03:42:46
As I said there is no workaround to this, you need to perform the decimal computations by your own and that requires a chinese work much like the coding worked out on jurassic computers using fixed binary assembly language. To do that you have to multiply the values by 100, perform your computations and eventually separate the integer and decimal part of the result. This requires batch substring manipulation. Can you master this coding?

Report •

Related Solutions

#4
April 18, 2011 at 03:54:32
Nope.

Thanks for your help :)


Report •

Ask Question