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


Java BigInteger floatValue()用法及代碼示例


java.math.BigInteger.floatValue()將此BigInteger轉換為浮點值。如果此函數返回的值太大而無法表示為浮點,則將視情況將其轉換為Float.NEGATIVE_INFINITY或Float.POSITIVE_INFINITY。此轉換有可能會丟失有關BigInteger值的精度的信息。

用法:

public float floatValue()

返回值:該方法返回一個浮點值,該值表示此BigInteger的浮點值。


例子:

Input: BigInteger1=32145
Output: 32145.0
Explanation: BigInteger1.floatValue()=32145.0.

Input: BigInteger1=32145535361361525377
Output: 3.2145535E19
Explanation: BigInteger1.floatValue()=3.2145535E19. This BigInteger is too big 
for a magnitude to represent as a float then it will be converted to 
Float.NEGATIVE_INFINITY or Float.POSITIVE_INFINITY as appropriate.

下麵的程序說明BigInteger類的floatValue()方法:

示例1:

// Java program to demonstrate floatValue() method of BigInteger 
  
import java.math.BigInteger; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Creating 2 BigInteger objects 
        BigInteger b1, b2; 
  
        b1 = new BigInteger("32145"); 
        b2 = new BigInteger("7613721"); 
  
        // apply floatValue() method 
        float floatValueOfb1 = b1.floatValue(); 
        float floatValueOfb2 = b2.floatValue(); 
  
        // print floatValue 
        System.out.println("floatValue of "
                           + b1 + " : " + floatValueOfb1); 
        System.out.println("floatValue of "
                           + b2 + " : " + floatValueOfb2); 
    } 
}
輸出:
floatValue of 32145 : 32145.0
floatValue of 7613721 : 7613721.0

示例2:當返回浮點太大而無法表示為浮點時,則為大。

// Java program to demonstrate floatValue() method of BigInteger 
  
import java.math.BigInteger; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // Creating 2 BigInteger objects 
        BigInteger b1, b2; 
  
        b1 = new BigInteger("32145535361361525377"); 
        b2 = new BigInteger("7613721535372632367351"); 
  
        // apply floatValue() method 
        float floatValueOfb1 = b1.floatValue(); 
        float floatValueOfb2 = b2.floatValue(); 
  
        // print floatValue 
        System.out.println("floatValue of "
                           + b1 + " : " + floatValueOfb1); 
        System.out.println("floatValue of "
                           + b2 + " : " + floatValueOfb2); 
    } 
}
輸出:
floatValue of 32145535361361525377 : 3.2145535E19
floatValue of 7613721535372632367351 : 7.6137214E21

參考: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#floatValue()



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger floatValue() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。