java.math.BigInteger floatValue()

Description

On this document we will be showing a java example on how to use the floatValue() method of BigInteger Class. Basically this method converts this BigInteger to a float. This conversion is similar to the narrowing primitive conversion from double to float as defined in section 5.1.3 of The Java™ Language Specification: if this BigInteger has too great a magnitude to represent as a float, it will be converted to Float.NEGATIVE_INFINITY or Float.POSITIVE_INFINITY as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the BigInteger value.

For reference the byte range is from 1.4E-45 to 3.4028235E38 and these information is readily available through the Float constants Float.MIN_VALUE and Float.MAX_VALUE.

Notes:

  • This method is specified by floatValue in class Number.

Method Syntax

public float floatValue()

Method Argument

Data Type Parameter Description
N/A N/A N/A

Method Returns

The doubleValue() method returns this BigInteger converted to a float.

Compatibility

Requires Java 1.1 and up

Java BigInteger floatValue() Example

Below is a java code demonstrates the use of floatValue() method of BigInteger class. The example presented might be simple however it shows the behavior of the floatValue() method.

package com.javatutorialhq.java.examples;

import java.math.BigInteger;
import java.util.Scanner;

/*
 * A java example source code to demonstrate
 * the use of floatValue() method of BigInteger class
 */

public class BigIntegerFloatValueExample {

	public static void main(String[] args) {	
		
		// Declare and initialize our BigInteger values
		BigInteger val1 = new BigInteger("12");
		BigInteger val2 = new BigInteger("4565451257489787");		
		
		// get the float equivalent of each BigInteger
		double floatVal1 = val1.floatValue();
		double floatVal2 = val2.floatValue();
		
		// print the operation
		System.out.println("val1 in float is "+floatVal1);
		System.out.println("val2 in float is "+floatVal2);		
		
	}

}

This example is a lot simpler than it looks. We only initialize two BigInteger  and then convert these into float. The result of the floatValue is printed out to simulate the conversion.

Sample Output

Below is the sample output when you run the above example.

BigInteger doubleValue() example output