当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java Math nextAfter()用法及代码示例

Java Math nextAfter() 方法返回在第二个参数方向上与第一个参数相邻的数字。

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

用法:

Math.nextAfter(start, direction)

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

参数:

  • start- 返回相邻号码的起始号码
  • direction- 指定哪个相邻的数量start将被退回

注意: 数据类型startdirection可以是浮点数或双精度数。

nextAfter() 返回值

  • 将与start 相邻的数字返回到direction

注意: 如果startdirection相等,则值等于direction被退回。

示例:Java 数学。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

  }
}

相关用法


注:本文由纯净天空筛选整理自 Java Math nextAfter()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。