Java教程

Java Math nextAfter()

Java Math nextAfter()

Java Math nextAfter() 方法返回第二个参数方向上与第一个参数相邻的数字。
即如果第一个参数是6.7,第二个参数是2.3,则6.7在方向的相邻数>2.3 是 6.699999999999999。
nextAfter() 方法的语法是:
Math.nextAfter(start, direction)
注意: nextAfter() 方法是一个静态方法。因此,我们可以使用类名 Math 直接调用该方法。

nextAfter() 参数

start-返回相邻编号的起始编号 direction-指定要返回哪个相邻的start
注意: start 和 direction 的数据类型可以是 float 或 double。

nextAfter() 返回值

返回与 start 相邻的数字,朝向 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
  }
}

推荐教程

Math.nextUp() Math.nextDown()
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4