BigInteger floatValue() Method in Java
Last Updated :
04 Dec, 2018
Improve
The java.math.BigInteger.floatValue() converts this BigInteger to a float value. If the value returned by this function 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. There is a chance that this conversion can lose information about the precision of the BigInteger value.
Syntax:
Java
Java
public float floatValue()Returns: The method returns a float value which represents float value for this BigInteger. Examples:
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.Below programs illustrate floatValue() method of BigInteger class: Example 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);
}
}
// 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);
}
}
Output:
Example 2: When return float is too big for a magnitude to represent as a float.
floatValue of 32145 : 32145.0 floatValue of 7613721 : 7613721.0
// 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);
}
}
// 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);
}
}
Output:
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#floatValue()
floatValue of 32145535361361525377 : 3.2145535E19 floatValue of 7613721535372632367351 : 7.6137214E21