BigDecimal floatValue() Method in Java with Examples
The java.math.BigDecimal.floatValue() converts this BigDecimal to a float. If this BigDecimal has too great 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 BigDecimal value.
Syntax:
public float floatValue()
Parameters: This function accepts no parameter.
Returns: The method returns a float value which represents float value for this BigDecimal.
Examples:
Input: BigDecimal1 = 1234 Output: 1234.0 Input: BigDecimal1 = 21545135451354545 Output: 2.15451365E16 Explanation: BigInteger1.floatValue() = 2.15451365E16. This BigDecimal 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 BigDecimal class:
Example 1:
// Java program to demonstrate // floatValue() method of BigDecimal import java.math.BigDecimal; public class GFG { public static void main(String[] args) { // For user input // Use Scanner or BufferedReader // Object of String created // Holds the value String input1 = "545456468445645468464645"; // Convert the string input to BigDecimal BigDecimal a = new BigDecimal(input1); // Using floatValue() method float f = a.floatValue(); // Display the result System.out.println(f); } } |
5.4545646E23
Example 2:
// Java program to demonstrate // floatValue() method of BigDecimal import java.math.BigDecimal; public class GFG { public static void main(String[] args) { // For user input // Use Scanner or BufferedReader // Object of String created // Holds the value String input1 = "984522"; // Convert the string input to BigDecimal BigDecimal a = new BigDecimal(input1); // Using floatValue() method float f = a.floatValue(); // Display the result System.out.println(f); } } |
984522.0
References: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#floatValue()
Recommended Posts:
- DoubleAccumulator floatValue() method in Java with Examples
- LongAdder floatValue() method in Java with Examples
- AtomicLong floatValue() method in Java with examples
- Number.floatValue() method in java with examples
- Byte floatValue() method in Java with examples
- LongAccumulator floatValue() method in Java with Examples
- AtomicInteger floatValue() method in Java with examples
- DoubleAdder floatValue() method in Java with Examples
- BigDecimal pow() method in Java with Examples
- BigDecimal add() Method in Java with Examples
- BigDecimal setScale() method in Java with Examples
- BigDecimal divide() Method in Java with Examples
- BigDecimal subtract() Method in Java with Examples
- BigDecimal toString() Method in Java with Examples
- BigDecimal toEngineeringString() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



