Java.math.BigInteger.floatValue() 方法



說明

java.math.BigInteger.floatValue() 將此 BigInteger 轉換為 float。此轉換類似於 double 到 float 的縮小原始轉換。

如果此 BigInteger 的幅度太大,無法表示為 float,它將適當地轉換為 Float.NEGATIVE_INFINITY 或 Float.POSITIVE_INFINITY。即使返回值有限,此轉換也可能會丟失有關 BigInteger 值精度的資訊。

宣告

以下是 java.math.BigInteger.floatValue() 方法的宣告。

public float floatValue()

指定由

Number 類中的 floatValue。

引數

不適用

返回值

此方法返回此 BigInteger 轉換為 float 後的值。

異常

不適用

示例

以下示例演示了 math.BigInteger.floatValue() 方法的用法。

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      // create 2 Float objects
      Float f1, f2;

      // assign values to bi1, bi2
      bi1 = new BigInteger("123");
      bi2 = new BigInteger("-123");

      // assign float values of bi1, bi2 to f1, f2
      f1 = bi1.floatValue();
      f2 = bi2.floatValue();

      String str1 = "Float value of " + bi1 + " is " +f1;
      String str2 = "Float value of " + bi2 + " is " +f2;

      // print f1, f2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

讓我們編譯並執行上述程式,這將產生以下結果 −

Float value of 123 is 123.0
Float value of -123 is -123.0
java_math_biginteger.htm
廣告
© . All rights reserved.