JAVA 关于四舍五入的问题 弱弱的问下,JAVA 里面有没有什么方法直接把一个小数 四舍五入 转化为整数? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 (1)import java.text.io; DecimalFormat df=new DecimalFormat("#.00"); System.out.println(df.format(d1)); System.out.println(df.format(d2)); (2) public int getRound(double dSource)...{int iRound//BigDecimal的构造函数参数类型是doubleBigDecimal deSource = new BigDecimal(dSource);//deSource.setScale(0,BigDecimal.ROUND_HALF_UP) 返回值类型 BigDecimal//intValue() 方法将BigDecimal转化为intiRound= deSource.setScale(0,BigDecimal.ROUND_HALF_UP).intValue();return iRound;} 转自 http://esffor.javaeye.com/blog/96158还有就是 你要按 第几位四舍五入 第一位 4.5=5第二位 4.49=4.5 DecimalFormat df = new DecimalFormat("#.00");System.out.println(df.format(4.5));System.out.println(df.format(4.492));System.out.println(df.format(4.496));这个简便。不过返回值变成String了。 要是就这么简单,直接Math.round吧。System.out.println(Math.round(4.5));System.out.println(Math.round(4.49)); 谢谢大家,受教了!!!CSDN 真棒! 四舍五入:Math.round天花板函数(向上取整):Math.floor 结果为 double,需要强转地板函数(向下取整):Math.cell 结果为 double,需要强转执行一下下面的代码就知道了:public class Test { public static void main(String[] args) throws UnsupportedEncodingException { double[] a = { -5.3, -6.5, -0.1, 0, 0.1, 5.3, 6.5 }; System.out.printf("%-10s %-10s %-10s %-10s%n", "Number", "round", "floor", "cell"); System.out.println("--------------------------------------------"); for(int i = 0; i < a.length; i++) { System.out.printf("%-10f %-10d %-10f %-10f%n", a[i], Math.round(a[i]), Math.floor(a[i]), Math.ceil(a[i])); } }} Number round floor cell ---------------------------------------------5.300000 -5 -6.000000 -5.000000 -6.500000 -6 -7.000000 -6.000000 -0.100000 0 -1.000000 -0.000000 0.000000 0 0.000000 0.000000 0.100000 0 0.000000 1.000000 5.300000 5 5.000000 6.000000 6.500000 7 6.000000 7.000000 说错了。floor 是向下取整,地板函数cell 是向上取整,天花板函数 问一个String 转 int的问题。。。 int和Integer的区别? 问一下关于权限的问题 java中int 类型的范围是:-2147483648 到2147483648 那他能表示最大的数是2147483648 吗?为什么啊? JAVA的入门基础一些精典 简单的排序问题 char数组的问题 为什么会不一样呢? 谁用过QueryString,需要加入什么包???? 当有三种条件组合查询时,是不是用if语句把三种条件的各种不同的组合列出,分别查询?当条件较多时,还是这样吗?(我是非计算机专业) 用正则表达式处理个字符串 请回答是或否:能否通过反射分析源代码,获得Map的key?
import java.text.io;
DecimalFormat df=new DecimalFormat("#.00");
System.out.println(df.format(d1));
System.out.println(df.format(d2)); (2)
public int getRound(double dSource)...{
int iRound
//BigDecimal的构造函数参数类型是double
BigDecimal deSource = new BigDecimal(dSource);
//deSource.setScale(0,BigDecimal.ROUND_HALF_UP) 返回值类型 BigDecimal
//intValue() 方法将BigDecimal转化为int
iRound= deSource.setScale(0,BigDecimal.ROUND_HALF_UP).intValue();
return iRound;
} 转自 http://esffor.javaeye.com/blog/96158还有就是 你要按 第几位四舍五入
第一位 4.5=5
第二位 4.49=4.5
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(4.5));
System.out.println(df.format(4.492));
System.out.println(df.format(4.496));
这个简便。不过返回值变成String了。
要是就这么简单,直接Math.round吧。
System.out.println(Math.round(4.5));
System.out.println(Math.round(4.49));
CSDN 真棒!
天花板函数(向上取整):Math.floor 结果为 double,需要强转
地板函数(向下取整):Math.cell 结果为 double,需要强转执行一下下面的代码就知道了:public class Test { public static void main(String[] args) throws UnsupportedEncodingException {
double[] a = { -5.3, -6.5, -0.1, 0, 0.1, 5.3, 6.5 };
System.out.printf("%-10s %-10s %-10s %-10s%n", "Number", "round", "floor", "cell");
System.out.println("--------------------------------------------");
for(int i = 0; i < a.length; i++) {
System.out.printf("%-10f %-10d %-10f %-10f%n", a[i], Math.round(a[i]), Math.floor(a[i]), Math.ceil(a[i]));
}
}
}
--------------------------------------------
-5.300000 -5 -6.000000 -5.000000
-6.500000 -6 -7.000000 -6.000000
-0.100000 0 -1.000000 -0.000000
0.000000 0 0.000000 0.000000
0.100000 0 0.000000 1.000000
5.300000 5 5.000000 6.000000
6.500000 7 6.000000 7.000000
说错了。floor 是向下取整,地板函数
cell 是向上取整,天花板函数