Java 程式用於舍入一個數字


java.lang.Math.round(float a) 返回最接近引數的 int。透過新增 1/2、取結果的向下舍入並將其轉型為 int 型別,結果舍入為一個整數。特殊情況 -

  • 如果引數為 NaN,則結果為 0。

  • 如果引數為負無窮大或任何小於或等於 Integer.MIN_VALUE 的值,則結果等於 Integer.MIN_VALUE 的值。

  • 如果引數為正無窮大或任何大於或等於 Integer.MAX_VALUE 的值,則結果等於 Integer.MAX_VALUE 的值。

示例

import java.util.Scanner;
public class RoundingDecimalPlaces {
   public static void main(String[] args) {
      System.out.println("Enter a string value ::");
      Scanner sc = new Scanner(System.in);
      float f = sc.nextFloat();
      System.out.println("Result of the given float after rounding:: " + Math.round(f));
   }
}

輸出

Enter a string value ::
44584.2257
Result of the given float after rounding:: 44584

更新於: 13-Mar-2020

583 次瀏覽

開啟您的職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.