如何将double
值转换为int
执行以下操作:
Double If x = 4.97542. Convert to int x = 4.
Double If x = 4.23544. Convert to int x = 4.
也就是说,答案总是四舍五入。
如何将double
值转换为int
执行以下操作:
Double If x = 4.97542. Convert to int x = 4.
Double If x = 4.23544. Convert to int x = 4.
也就是说,答案总是四舍五入。
如果显式转换 double
为int
,小数部分将被截断。例如:
int x = (int) 4.97542; //gives 4 only
int x = (int) 4.23544; //gives 4 only
此外,如果您想要返回值,您还可以使用Math.floor()
方法对值进行舍double
入。
如果双精度为Double
大写D(盒装原始值):
Double d = 4.97542;
int i = (int) d.doubleValue();
// or directly:
int i2 = d.intValue();
如果 double 已经是一个原始的double
,那么你只需转换它:
double d = 4.97542;
int i = (int) d;
double myDouble = 420.5;
//Type cast double to int
int i = (int)myDouble;
System.out.println(i);
双精度值是 420.5,应用程序打印出整数值 420
另一个选项 usingDouble
或double
is use Double.valueOf(double d).intValue();
。简单干净
我认为我有更好的输出,特别是对于双数据类型排序。
尽管此问题已被标记为已回答,但也许这对其他人有帮助;
Arrays.sort(newTag, new Comparator<String[]>() {
@Override
public int compare(final String[] entry1, final String[] entry2) {
final Integer time1 = (int)Integer.valueOf((int) Double.parseDouble(entry1[2]));
final Integer time2 = (int)Integer.valueOf((int) Double.parseDouble(entry2[2]));
return time1.compareTo(time2);
}
});