57

How to convert from float to bigDecimal in java?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
akp
  • 1,823
  • 7
  • 26
  • 29

4 Answers4

73
BigDecimal value = new BigDecimal(Float.toString(123.4f));

From the javadocs, the string constructor is generally the preferred way to convert a float into a BigDecimal, as it doesn't suffer from the unpredictability of the BigDecimal(double) constructor.

Quote from the docs:

Note: For values other float and double NaN and ±Infinity, this constructor is compatible with the values returned by Float.toString(float) and Double.toString(double). This is generally the preferred way to convert a float or double into a BigDecimal, as it doesn't suffer from the unpredictability of the BigDecimal(double) constructor.

aioobe
  • 413,195
  • 112
  • 811
  • 826
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 8
    But converting a `float` to a String explicitly doesn't help you solve the unpredictability automatically - you need to take care to format the value correctly (rounding etc.). – Jesper Sep 30 '10 at 08:58
  • How to convert and format 9.991f to 910 and 9.99f to 9.99? – Jobin Aug 10 '21 at 08:45
  • See: https://docs.oracle.com/javase/8/docs/api/java/lang/Float.html#toString-float- – Josue Abarca Apr 25 '23 at 21:49
29
float f = 45.6f;
BigDecimal bd = BigDecimal.valueOf(f);

Quote from documentations:

Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).

Reference: BigDecimal (Java Platform SE 6)

Spacemonkey
  • 1,725
  • 3
  • 20
  • 44
9

For a precision of 3 digits after the decimal point:

BigDecimal value = new BigDecimal(f,
        new MathContext(3, RoundingMode.HALF_EVEN));
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
  • How to convert and format 9.991f to 910 and 9.99f to 9.99? – Jobin Aug 10 '21 at 08:45
  • @Jobin I don't get your question. Why would 9.991f be converted to 910? Did you mean 10 instead of 910? in that case, you would need `new MathContext(2, RoundingMode.UP)` or `new MathContext(2, RoundingMode.CEILING)` depending on how you want to deal with the negative numbers. – Maurice Perry Sep 17 '22 at 13:00
2

This is upto my knowledge :

   public static BigDecimal floatToBigDecimal(Float a){


    if(a == null || a.isInfinite() || a.isNaN()){
        return BigDecimal.ZERO;
    }
    try{
        return BigDecimal.valueOf(a);

    }catch(Exception e){
        return BigDecimal.ZERO;
    }

}

*Note:This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).

public static BigDecimal valueOf(double val)

Parameters:
val - double to convert to a BigDecimal.
Returns:
a BigDecimal whose value is equal to or approximately equal to the value of val.
Throws:
NumberFormatException - if val is infinite or NaN.
Since:
1.5

I have checked whether Infinite or Not a Number, so that there is less chances of NumberFormatException