目录

static int round(float a)

描述 (Description)

java.lang.StrictMath.round(float a)方法返回与参数最接近的int。 结果通过添加1/2来舍入为整数,取结果的底限,并将结果转换为int类型。它包括这些情况 -

  • 如果参数为NaN,则结果为0。
  • 如果参数为负无穷大或任何小于或等于Integer.MIN_VALUE值的值,则结果等于Integer.MIN_VALUE的值。
  • 如果参数为正无穷大或任何大于或等于Integer.MAX_VALUE值的值,则结果等于Integer.MAX_VALUE的值。

声明 (Declaration)

以下是java.lang.StrictMath.round()方法的声明

public static int round(float a)

参数 (Parameters)

a - 这是要舍入为整数的浮点值。

返回值 (Return Value)

此方法返回舍入为最接近的int值的参数的值。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.lang.StrictMath.round()方法的用法。

package com.iowiki;
import java.lang.*;
public class StrictMathDemo {
   public static void main(String[] args) {
      float f1 = 98.22f , f2 = 23.44f;
      // returns the closest int to the argument 
      int roundValue = StrictMath.round(f1); 
      System.out.println("closest int value to " + f1 + " = " + roundValue);
      roundValue = StrictMath.round(f2); 
      System.out.println("closest int value to " + f2 + " = " + roundValue);
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

closest int value to 98.22 = 98
closest int value to 23.44 = 23
↑回到顶部↑
WIKI教程 @2018