参考链接:http://blog.youkuaiyun.com/wanlixingzhe/article/details/7359809
参考链接:http://bbs.youkuaiyun.com/topics/390677448(6楼)
参考链接:http://blog.sina.com.cn/s/blog_6940cab30101hji5.html
最近在做一个计算的时候用到了取整取余的计算,这里对取整、取余、取模做一下总结~~~
1、取整
- int a = 10;
- int b = 3;
- double c = a / b;//c = (10/3) = (double)3 = 3.0
- System.out.println(c);
- int a = 10;
- int b = 3;
- double c = (double) a / b;//c = (10.0/3) = 3.333333
- System.out.println(c);
2、取余(运算符为%)
表达式:result = num1 % num2
- double result_double = 19 % 6.7;//5.6
- int result_int = (int) (19 % 6.7);//5
3、取模
在网上找了一下关于取模的资料:取模和取余是两回事,在JAVA、C、C++里只有取余,操作符% ,英文remainder;在Python里%号是取模运算,英文modulus;在matlab里面有一个rem和mod函数,分别对应取余和取模运算。
取余: rem(3,2)=1 rem(-3,-2)=-1 rem(3,-2)=1 rem(-3,2)=-1
取模: mod(3,2)=1 mod(-3,-2)=-1 mod(3,-2)=-1 mod(-3,2)=1
总结:rem结果的符号与被除数相同;mod结果的符号与除数相同。