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

BigDecimal is a big integer with arbitrary-precision signed decimal numbers.

It contains a 32-bit integer and an unscaled decimal value. It is defined in java. math class. It is used in product prices and scientific calculations

Float is used to store 32-bit floating-point numbers.

This tutorial shows you how to convert BigDecimal to Float in java.

Note: If you are using currency values BigDecimal and converting to Float, You lose precision and It gives wrong results in currency processing. You can also check my previous posts on the BigInteger class in java.

How to convert bigdecimal to Float in java

BigDecimal has a method floatValue that returns the float value.

It returns Float.NEGATIVE_INFINITY or Float.POSITIVE_INFINITY based on how big is value in BigDecimal.

Here is a syntax

    public float floatValue()

Here is an example program code

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.lang.BigDecimal
        Float d = order.floatValue();
        System.out.println(d); //789.124
        System.out.println(d.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 shows you ways to convert Float to BigDecimal in java We can do it in two ways

  • BigDecimal Constructor:

    With the constructor, You can convert as String or double parameters. Convert the Float into a string and returns BigDecimal Object. Similarly, Since a float is automatically converted to double, So you can pass double parameters to the constructor

    public BigDecimal(String val)
    public BigDecimal(Double val)
    
  • valueOf method: The static method takes the Float value and returns 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 below values

Following are steps

  • Create a BigDecimal object
  • Pass Double value,MathContext object to constructor
      public BigDecimal(double val, MathContext mc)
  • MathContext 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

Learned multiple ways to Convert BigDecimal to or from Float in java. Also, converting bigdecimal to Float results in the addition of precision values and trying to avoid currency calculations.