首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

急求!Math类中round(double a)与round(float a)方法的不同,该如何解决

2012-05-07 
急求!!!Math类中round(double a)与round(float a)方法的不同public class Test2 {/** * @param args */pub

急求!!!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

热点排行