Java BigDecimal floatValue() Method with Example

BigDecimal Class floatValue() method: Here, we are going to learn about the floatValue() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 03, 2020

BigDecimal Class floatValue() method

  • floatValue() method is available in java.math package.
  • floatValue() method is used to convert a BigDecimal to a float value and when this BigDecimal magnitude is not in a range of float so it will be changed to Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY.
  • floatValue() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • floatValue() method does not throw an exception at the time of converting BigDecimal to the float.

Syntax:

    public float floatValue();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is float, it returns the float representation of this BigDecimal.

Example:

// Java program to demonstrate the example 
// of float floatValue() method of BigDecimal

import java.math.*;

public class FloatValueOfBD {
    public static void main(String args[]) {
        // Initialize two variables first is
        // of "double" and second is of "String" type
        double val = 1255468.00;
        String str = "100";

        // Initialize two BigDecimal objects  
        BigDecimal b_dec1 = new BigDecimal(val);
        BigDecimal b_dec2 = new BigDecimal(str);

        // convert this BigDecimal (b_dec1) into
        // a float, variable named f_conv
        float f_conv = b_dec1.floatValue();
        System.out.println("b_dec1.floatValue(): " + f_conv);

        // convert this BigDecimal (b_dec2) into
        // a float, variable named f_conv
        f_conv = b_dec2.floatValue();
        System.out.println("b_dec2.floatValue(): " + f_conv);
    }
}

Output

b_dec1.floatValue(): 1255468.0
b_dec2.floatValue(): 100.0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.