哪位解释一下Math.round/floor/round的用法 谢谢。明天再加100分酬谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ceil和floor是两个相对的方法,ceil是求大于等于参数的最小整数,floor是求小于等于参数的最大整数.具体说明见文档.round这样计算:public static int round(float a)返回最接近参数的 int。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为 int 类型。换句话说,结果等于以下表达式的值: (int)Math.floor(a + 0.5f) double floor(double) 返回不大于该参数的最大整数 (最接近正无穷)的 double 型值。int round(double) 返回与该参数最接近的 long 型数。 (四舍五入)long round(float) 返回与该参数最接近的 int 型数。(四舍五入) ceil,floorceil就是靠近负坐标floor就是靠近正坐标这样好记点 ceil,floor ceil就是靠近负坐标 floor就是靠近正坐标 这个说的不错,不过记得是整数哦。和数据库里的函数其实是一样的 public class MyTest { public static void main(String[] args) { double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 }; for (double num : nums) { test(num); } } private static void test(double num) { System.out.println("Math.floor(" + num + ")=" + Math.floor(num)); System.out.println("Math.round(" + num + ")=" + Math.round(num)); System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num)); }}运行结果Math.floor(1.4)=1.0Math.round(1.4)=1Math.ceil(1.4)=2.0Math.floor(1.5)=1.0Math.round(1.5)=2Math.ceil(1.5)=2.0Math.floor(1.6)=1.0Math.round(1.6)=2Math.ceil(1.6)=2.0Math.floor(-1.4)=-2.0Math.round(-1.4)=-1Math.ceil(-1.4)=-1.0Math.floor(-1.5)=-2.0Math.round(-1.5)=-1Math.ceil(-1.5)=-1.0Math.floor(-1.6)=-2.0Math.round(-1.6)=-2Math.ceil(-1.6)=-1.0 参考这个 http://www.java2000.net/p9612 [http://www.google.cn IO流问题 有关java的问题 启发式算法 如何操作包含在一个容器中,并且没引用名的对象 大家帮我看看我这个程序,很急迫! 关于swt,table数据实时更新。 怎样操纵double类型,进行计算啊? 书上一道题:基本数据类型和复合数据类型变量,系统处理有何不同? 请问:4种顶级类修饰符具体是什么? 现在选中的菜单项前面加个标志,请问应该怎样做? 每个新手都要搞懂弄明白的 jmf 相关说明和教程
round这样计算:public static int round(float a)返回最接近参数的 int。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为 int 类型。换句话说,结果等于以下表达式的值: (int)Math.floor(a + 0.5f)
返回不大于该参数的最大整数 (最接近正无穷)的 double 型值。
int round(double)
返回与该参数最接近的 long 型数。 (四舍五入)
long round(float)
返回与该参数最接近的 int 型数。(四舍五入)
ceil就是靠近负坐标
floor就是靠近正坐标
这样好记点
ceil就是靠近负坐标
floor就是靠近正坐标 这个说的不错,不过记得是整数哦。
和数据库里的函数其实是一样的
public class MyTest {
public static void main(String[] args) {
double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
for (double num : nums) {
test(num);
}
} private static void test(double num) {
System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
System.out.println("Math.round(" + num + ")=" + Math.round(num));
System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
}
}运行结果
Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0