# Is it possible to make batch files take fractions into account?

When you put a command to evaluate and equation, like (2/3)+1, the answer would just appear as 1. Even when you put an equation like (2/3+1)*3, where the answer is an integer 5, the answer would come out as 3. Any idea how to make batch files calculate the fractions together with the integers?

active| newest | oldestFirst off, SET is not built to work with fractions. In fact, it doesn't handle math very well at all. It will round-down it's answer to the nearest whole number. That means 1/3 will come out as zero, 3/2 will come out as 1, etc... The only way around this is to alter your math. For example:

I'd like to find out what 2/3 is, to 3 decimal places. If I just try to divide 2 by 3, I get this:

C:\>set /A _result=2/3

0 <- this is 0.666... but it has been rounded down to the whole integer 0 by SET.

I can get a more useful answer by slightly altering my process. I know that SET will not give me any decimal places beyond zero, so I'll change the answer set in an easily correctable way. So before I start, I multiply the numerator by 1000.

C:\>set /A _result=2000/3

666

To correct this to answer the original question, simply divide by 1000. Since SET can't handle decimals, just remember that it's as simple as moving the decimal to the left three times. You can do that in your head, or do it later in the program using another method. The answer (to three decimal places) is 0.666

Now, that's just a simple example on how you can use creative math to do more with a limited set of capabilities. Always practicing math and learning little tricks like this will keep you from running into roadblocks in all kinds of situations. Best of luck.

There are alternatives, such as finding a command-line program that better handles math and using that from within your batch file, or finding a programming language that handles math easily and learning it. I've had great luck with PHP.

Anyway... I'm going to do the same trick for you again, using your own equations.

C:\>set /A _result=(2/3)+1

1 <ACK! Ugly response.

C:\>set /A _result=(2000/3)+1

667 <Wait! That's wrong?! Oh right, the rest of the equation's decimal place needs to be shifted too.

C:\>set /A _result=(2000/3)+1000

1666 <Perfect. This translates to 1.666

And your other equation:

C:\>set /A _result=(2000/3+1000)*3

4998 < This means 4.998 which is roundable to 5 (the right answer).

You could also solve the equation in other ways. This was just one simple example.

canput the SET command in a batch, but being in or out of a batch is irrelevant here. The question would have been better titled "Is it possible to make the MS DOS SET function take fractions into account?"L