ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.