日期:2014-05-20  浏览次数:20634 次

急求!!!Math类中round(double a)与round(float a)方法的不同
public class Test2 {

/**
* @param args
*/
public static void main(String[] args) {
long x=(long) 12.5;
long y=(long) -12.5;
double m=12.5;
double n=-12.5;

System.out.println("x="+Math.round(x));  
System.out.println("y="+Math.round(y));
System.out.println("m="+Math.round(m));
System.out.println("n="+Math.round(n));

}

}

结果:
x=12
y=-12
m=13
n=-12

------解决方案--------------------
问题不是出在 round 上,而是你自己的问题。

long x=(long) 12.5; // 这里就已经丢失了0.5了,因为你强转了,long也是(长)整形,只能存储整数!
System.out.println(x); // 你用这句话看看是多少,必然是 12