Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I set a precision to the SET arithmetic operation on divisions?

eg. SET /A t=3/4

But that gives result as '0' and not '0.75' ( I want at least 2 decimal places )

Or is there any other method for such arithmetic operations (using other than "set /A" ) to happen in Batch files?

share|improve this question
2  
SET /A only works on integers. So there is no way to get 0.75 using that –  Nifle Jun 16 '11 at 6:54
    
@Nifle: Post it as an answer before I do. –  grawity Jun 16 '11 at 7:37
    
@grawity - will do –  Nifle Jun 16 '11 at 8:54

3 Answers 3

up vote 3 down vote accepted

If you're using Microsoft's CMD, then what Nifle said applies. However, if you are using JP Software's TCC/LE to replace it, not only is there another way to do arithmetic in command scripts, but it is not limited to integer arithmetic and the precision of the calculations is controllable.

TCC/LE supports the /A option to the SET command, of course. But, as the documentation states, one can also perform arithmetic with the @EVAL[] variable function:

SET T=%@EVAL[3/4]

The output precision of the %@EVAL[] calculation is a controllable option, settable in three ways:

share|improve this answer
    
Thanks.. i hope i can work with this smoothly.. looks promising. –  Power-Inside Jun 16 '11 at 12:47

SET /A only works on integers. So there is no way to get 0.75 using that.
And I know of no other way to do arithmetic in batch files.

share|improve this answer
    
really? 2 upvotes? this wasn't even a solution to my problem. it was better off in the comments btw.. JdeBP's workaround is good enough (below). –  Power-Inside Jun 16 '11 at 12:41
    
In fairness, and as I noted in my answer, what Nifle said applies if one only has Microsoft's CMD. "You cannot do what you want with the command interpreter that you have." is a valid answer. –  JdeBP Jun 16 '11 at 12:54

You might not be able to actually do what you want, but you may be able to approximate the desired results by multiplying and dividing by powers of 10, and doing string manipulation.  For example, consider this divide.bat:

@echo off
set /a q=%1 * 1000 / %2
echo q = %q:~0,-3%.%q:~-3%

%variable_name:~start_pos,length% is Command Prompt variable substring notation.  Negative numbers are relative to the length of the string (variable value); a missing length means the rest of the string.

Here are some sample results:

divide 8 4    ->    q = 2.000
divide 6 4    ->    q = 1.500
divide 3 4    ->    q = .750

In the last example (your example), q is actually 750.  If you want to compute 75% of something, either

  • multiply by %q% and divide by 1000, or
  • multiply by %q% and remember that the result is 1000× its true value, and you need to use the substring notation when you display it.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.