当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。