Java Convert Float to BigInteger

Tags: float BigInteger

In this Java core tutorial we learn how to convert a float value into BigInteger value in Java programming language.

How to convert float to BigInteger in Java

In Java, to convert a float value to BigInteger we can use the following steps.

  • Step 1: Cast the float value to a long value.
  • Step 2: Use the BigInteger.valueOf(long val) static method to convert the long value of step 1 to a BigInteger value.

ConvertFloatToBigIntegerExample1.java

import java.math.BigInteger;

public class ConvertFloatToBigIntegerExample1 {
    public static void main(String... args) {
        float floatValue = 9999f;

        // Convert Float to BigInteger
        long longValue = (long)floatValue;
        BigInteger bigIntegerValue = BigInteger.valueOf(longValue);

        System.out.println("float value: " + floatValue);
        System.out.println("BigInteger value: " + bigIntegerValue);
    }
}
The output as below.
float value: 9999.0
BigInteger value: 9999

Happy Coding 😊

Java Convert Float to Hexadecimal String