How to Convert BigDecimal to Float or Float to BigDecimal in java with examples

BigDecimal in Java

BigDecimal is a class that represents arbitrary-precision signed decimal numbers. It comprises a 32-bit integer and an unscaled decimal value. This class is defined in the java.math package and finds applications in scenarios such as product prices and scientific calculations.

On the other hand, Float is utilized for storing 32-bit floating-point numbers.

This tutorial will guide you on how to convert a BigDecimal to a Float in Java.

Important Note: When dealing with currency values, if you use BigDecimal and decide to convert to Float, precision is lost. This can lead to inaccurate results, especially in currency processing. It is advisable to exercise caution in such scenarios.

You can also check my previous posts on the BigInteger class in java.

How to Convert BigDecimal to Float in Java

The BigDecimal class in Java provides a method named floatValue for converting BigDecimal to a float value. Depending on the magnitude of the BigDecimal value, it returns either Float.NEGATIVE_INFINITY or Float.POSITIVE_INFINITY.

The syntax for the floatValue method is as follows.

Here is a syntax

    public float floatValue()

Here’s an example program demonstrating the conversion.

import java.math.BigDecimal;

public class BigDecimalTest {
    public static void main(String[] args) {
        BigDecimal order = new BigDecimal(789.124);
        System.out.println(order); // 789.124000000000023646862246096134185791015625
        System.out.println(order.getClass()); // class java.math.BigDecimal
        Float floatValue = order.floatValue();
        System.out.println(floatValue); // 789.124
        System.out.println(floatValue.getClass()); // class java.lang.Float
    }
}

Output:

789.124000000000023646862246096134185791015625
class java.math.BigDecimal
789.124
class java.lang.Float

How to Convert Float to BigDecimal in Java

This section explains two ways to convert a Float to BigDecimal in Java.

  • Using BigDecimal Constructor

    With the constructor, you can convert either a String or double parameter. Convert the Float into a string and return a BigDecimal object. Since a float is automatically converted to double, you can pass double parameters to the constructor.

    public BigDecimal(String val)
    public BigDecimal(Double val)
    
  • Using valueOf method The static valueOf method takes the Float value and returns a BigDecimal object.

        public static BigDecimal valueOf(double val)

    Here is a complete program code

    import java.math.BigDecimal;
    
    public class BigDecimalTest {
        public static void main(String[] args) {
    
            Float f = 467.123f;
            System.out.println(f); //467.123
            System.out.println(f.getClass()); //class java.lang.Float
    
            // using String constructor
            BigDecimal order = new BigDecimal(f.toString());
            System.out.println(order); //467.123
            System.out.println(order.getClass()); //class java.lang.BigDecimal
    
            // using double constructor
            BigDecimal order1 = new BigDecimal(f);
            System.out.println(order1); //467.12298583984375
            System.out.println(order1.getClass()); //class java.lang.BigDecimal
    
            // using valueOf method
            BigDecimal order2 = BigDecimal.valueOf(f);
            System.out.println(order2); //467.12298583984375
            System.out.println(order2.getClass()); //class java.lang.BigDecimal
    
    
        }
    }

    Output:

    467.123
    class java.lang.Float
    467.123
    class java.math.BigDecimal
    467.12298583984375
    class java.math.BigDecimal
    467.12298583984375
    class java.math.BigDecimal

    In the program, precision is a long value. You can restrict precision or decimal values using the MathContext object and specifying the desired precision.

    Here’s an example code to convert float to BigDecimal with 3 decimal places:

    Following are steps

    • Create a BigDecimal object
    • Pass Double value,MathContext object to constructor
          public BigDecimal(double val, MathContext mc)
  • Using MathContext class It takes a precision value which is of total digits before and after dot (.)operator and pass RoundingMode.HALF_EVEN

    Here is an example code to convert float to bigdecimal with 3 decimal places.

    import java.math.BigDecimal;
    import java.math.MathContext;
    import java.math.RoundingMode;
    
    public class BigDecimalTest {
        public static void main(String[] args) {
    
            Float f = 467.123f;
            System.out.println(f); //467.123
            System.out.println(f.getClass()); //class java.lang.Float
    
            // using MathContext method to limit 2 decimal places
            BigDecimal order = new BigDecimal(f.floatValue(),
                    new MathContext(6, RoundingMode.HALF_EVEN));
            System.out.println(order); //467.123
            System.out.println(order.getClass()); //class java.lang.BigDecimal
    
    
        }
    }

Summary

In summary, we’ve learned multiple ways to convert BigDecimal to or from Float in Java. It’s crucial to be cautious when converting BigDecimal to Float to avoid precision loss, especially in currency calculations.