Java中的BigInteger floatValue()方法

这个 JAVA数学大整数。浮动值() 将此BigInteger转换为浮点值。如果此函数返回的值太大,无法用浮点表示大小,则会将其转换为浮点。负无穷大或浮点数。适当的正无穷大。这种转换可能会丢失有关BigInteger值精度的信息。

null

语法:

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()

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享