温馨提示×

java中double转换为int怎么实现

小亿
123
2023-11-08 19:11:20
栏目: 编程语言

在Java中将double类型转换为int类型可以使用强制类型转换或者使用Math类提供的方法进行转换。

  1. 强制类型转换:

    double d = 3.14;
    int i = (int) d;
    
  2. 使用Math类提供的方法:

    • 使用Math.floor()方法将double向下取整转换为int
      double d = 3.14;
      int i = (int) Math.floor(d);
      
    • 使用Math.ceil()方法将double向上取整转换为int
      double d = 3.14;
      int i = (int) Math.ceil(d);
      
    • 使用Math.round()方法将double四舍五入转换为int
      double d = 3.14;
      int i = (int) Math.round(d);
      

0