2741Views8Replies

# 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?

## Discussions

The math handling is a function of windows&DOS's "SET" command, not of Batch. Batch is not a programming language and doesn't contain any commands or powers on it's own. A batch file is no more than a list of commands to run by the command interpreter.

First 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.

Multiplying it by 1000 is a great idea! I'll think of a way to put it into the batch... I was trying to get the batch to calculate something for a game... The game goes like this... Pick 4 numbers from 1-10 (numbers can be repeated)... Using each number once (not more not less), and using only + - * /, we have to try and get the number 28... Some combinations are impossible though, so I was trying to make a batch which can calculate if it is possible to get 28...

Batch files run batches of commands they don't do math. What command are you using for this - (exact specific line) L

yes they do, i believe that with set /a abc=equation you can do math, i don't think that it takes fractions into acount, but you can just have an imaginary dot, like instead off 129.3664 you use 1293664....

That is running the SET command. You

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

If you tell me what it is you want to do I'll do it for you in Perl or Python.

I would try using whole numbers that are larger than the numbers that you are using (for example, instead of 29.99 you use 2999) and then divide by 1/100 (in this case. add 1 zero for every number behind the decimal.)

What're you trying to do with your batch file?