强制类型转换(Type Casting)
这是最直接、最底层的方法,它会直接将 double 的整数部分截取出来,丢弃所有的小数部分。

(图片来源网络,侵删)
语法
(int) doubleValue;
特点
- 直接截断:只保留整数部分,没有四舍五入。
- 性能最高:这是 JVM 原生支持的操作,速度最快。
- 不检查范围:
double的值超出了int的表示范围(-2,147,483,648到2,147,483,647),它不会抛出异常,而是得到一个无意义的、回绕的整数结果,这在某些情况下可能导致难以发现的 Bug。
示例代码
public class DoubleToIntExample {
public static void main(String[] args) {
double positiveDouble = 99.89;
double negativeDouble = -99.89;
double largeDouble = 1.5e10; // 15,000,000,000,远大于 int 的最大值
// 强制转换,直接截断小数部分
int positiveInt = (int) positiveDouble;
int negativeInt = (int) negativeDouble;
int largeInt = (int) largeDouble;
System.out.println("原始值: " + positiveDouble + " -> 强制转换后: " + positiveInt); // 输出: 99
System.out.println("原始值: " + negativeDouble + " -> 强制转换后: " + negativeInt); // 输出: -99
System.out.println("原始值: " + largeDouble + " -> 强制转换后: " + largeInt); // 输出: -2147483648 (发生了整数回绕)
}
}
Math.floor()(向下取整)
如果你希望将一个 double 向下取整到最接近的 int,可以使用 Math.floor(),这个方法会返回一个 double 类型的结果,所以还需要再进行一次强制转换。
语法
(int) Math.floor(doubleValue);
特点
- 向下取整:对于正数,效果等同于截断;对于负数,会向更小的方向取整。
Math.floor(99.89)->0Math.floor(-99.89)->-100.0(因为 -100 比 -99 小)
- 不检查范围:同样,如果值超出
int范围,也会发生回绕。
示例代码
public class MathFloorExample {
public static void main(String[] args) {
double positiveDouble = 99.89;
double negativeDouble = -99.89;
int positiveInt = (int) Math.floor(positiveDouble);
int negativeInt = (int) Math.floor(negativeDouble);
System.out.println("原始值: " + positiveDouble + " -> Math.floor() 后: " + positiveInt); // 输出: 99
System.out.println("原始值: " + negativeDouble + " -> Math.floor() 后: " + negativeInt); // 输出: -100
}
}
Math.round()(四舍五入)
这是最常用、最符合直觉的方法,因为它会进行四舍五入,然后返回一个 long 类型的结果。double 的值在 int 的范围内,你可以安全地将其转换为 int。
语法
(int) Math.round(doubleValue);
注意:
Math.round()的返回值是long,因为当double值很大时(147483647E9),四舍五入后的结果可能超出int的范围,所以正确的做法是先将其赋给一个long变量,或者直接强制转换(如果确定结果在int范围内)。
特点
- 四舍五入:这是最符合数学直觉的转换方式。
Math.round(99.2)->99Math.round(99.5)->100
- 返回
long:这是一个重要的安全特性,可以防止大数转换时发生静默的回绕错误,如果结果超出了long的范围,才会发生回绕。 - 不检查范围:虽然返回
long更安全,但如果最终强制转换成int,且值超出了int范围,仍然会发生回绕。
示例代码
public class MathRoundExample {
public static void main(String[] args) {
double d1 = 99.49;
double d2 = 99.50;
double d3 = 99.89;
// Math.round() 返回的是 long,可以安全地存储大数
long roundedLong1 = Math.round(d1);
long roundedLong2 = Math.round(d2);
long roundedLong3 = Math.round(d3);
// 如果确定结果在 int 范围内,可以直接强制转换
int roundedInt1 = (int) Math.round(d1);
int roundedInt2 = (int) Math.round(d2);
int roundedInt3 = (int) Math.round(d3);
System.out.println(d1 + " -> 四舍五入后 (long): " + roundedLong1); // 输出: 99
System.out.println(d2 + " -> 四舍五入后 (long): " + roundedLong2); // 输出: 100
System.out.println(d3 + " -> 四舍五入后 (long): " + roundedLong3); // 输出: 100
System.out.println("--------------------");
System.out.println(d1 + " -> 四舍五入后 (int): " + roundedInt1); // 输出: 99
System.out.println(d2 + " -> 四舍五入后 (int): " + roundedInt2); // 输出: 100
System.out.println(d3 + " -> 四舍五入后 (int): " + roundedInt3); // 输出: 100
}
}
安全转换:处理超出范围的情况
在实际应用中,特别是处理用户输入或外部数据时,double 的值可能非常大,直接转换可能会导致回绕错误,为了安全地转换,应该先检查范围。

(图片来源网络,侵删)
推荐的安全转换方法
public class SafeDoubleToIntConversion {
public static void main(String[] args) {
double d = 1.5e10; // 15,000,000,000
int result;
// 检查 double 值是否在 int 的范围内
if (d >= Integer.MIN_VALUE && d <= Integer.MAX_VALUE) {
// 在范围内,可以安全地进行四舍五入
result = (int) Math.round(d);
System.out.println("安全转换成功: " + result);
} else {
// 不在范围内,进行错误处理
System.out.println("错误: double 值 " + d + " 超出了 int 的范围!");
// 可以选择抛出异常或返回一个默认值
// throw new ArithmeticException("Value out of int range");
// result = 0; // 返回默认值
}
}
}
总结与如何选择
| 方法 | 语法 | 行为 | 性能 | 范围检查 | 适用场景 |
|---|---|---|---|---|---|
| 强制转换 | (int) d |
直接截断小数部分 | 最快 | 无 | 当你明确知道只需要整数部分,且不关心四舍五入时,性能要求极高的场景。 |
Math.floor() |
(int) Math.floor(d) |
向下取整 | 较慢(有方法调用开销) | 无 | 当你需要向负无穷方向取整时,例如在金融计算中处理某些边界情况。 |
Math.round() |
(int) Math.round(d) |
四舍五入 | 较慢(有方法调用开销) | 无(但返回 long 更安全) |
最常用,当你需要一个符合数学直觉的、最接近的整数时。推荐首选。 |
| 安全转换 | if (check) { (int) Math.round(d); } |
四舍五入 | 最慢(有额外判断) | 有 | 生产环境首选,当数据来源不可控,必须确保转换结果有效时。 |
- 如果只是想快速去掉小数点,用 强制转换。
- 如果想得到最接近的整数,用
Math.round()。 - 如果代码要给别人用,或者处理的是不可信的数据,一定要用 安全转换 的方式。

(图片来源网络,侵删)
