Main Content

Question on division (Java) - Also Perl, PHP, Python ...

Archive - Originally posted on "The Horse's Mouth" - 2007-11-28 14:47:23 - Graham Ellis

Q:



What's the difference between % and /?

A:



/ returns the result of a division
% returns the remainder when you do a division.

Divide 18 by 7 you get 2, with a remainder of 4 (i.e. 4 left over).

so 18/7 give 2, and 18%7 gives 4.




And from Tcl/Tk and Expect... you can see the float v integer considerations of a division!

% expr 18 / 7
2
% expr 18. / 7
2.57142857143
% expr 18 % 7
4
%