當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。