Java Math nextAfter()

也就是说,如果第一个参数是 6.7,第二个参数是 2.3,那么在 6.7 朝向 2.3 方向上的相邻数是 6.699999999999999

nextAfter() 方法的语法是

Math.nextAfter(start, direction)

注意nextAfter() 方法是一个静态方法。因此,我们可以直接使用类名 Math 调用该方法。


nextAfter() 参数

  • start - 返回其相邻数的起始数
  • direction - 指定要返回 start 的哪个相邻数

注意startdirection数据类型可以是 float 或 double。


nextAfter() 返回值

  • 返回 start 朝向 direction 的相邻数

注意:如果 startdirection 相等,则返回与 direction 相等的值。


示例:Java Math.nextAfter()

class Main {
  public static void main(String[] args) {

    // float arguments
    // returns the smaller adjacent number
    float start1 = 7.9f;
    float direction1 = 3.3f;
    System.out.println(Math.nextAfter(start1, direction1));  // 7.8999996

    // double arguments
    // returns the larger adjacent number
    double start2 = 7.9f;
    double direction2 = 9.8f;
    System.out.println(Math.nextAfter(start2, direction2));  // 7.9000000953674325

  }
}

另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战