Not sure about you, but I find myself in frequent need of converting some other Java object to BigDecimal.

And it's not always intuitive as to how to do that.

So I'll 'splain it here.

What Is BigDecimal?

In a nutshell: it's the means to represent exact numbers in the Java programming language. Especially if you're working with decimals.

There are other numeric representations, like double, that give you a certain level of precision.

But if you're in the market for maximum precision, go with BigDecimal.

For example, if you're implementing a geometry-based solution that needs to carry Pi out to the 100th decimal point, BigDecimal is your baby.

Don't try that with float.

But even if you don't go to that extreme, you'll find that float and double often give you rounding errors. With BigDecimal you have full control over how to handle rounding.

So BigDecimal is a necessary and helpful beast of burden. That's why a lot of frameworks and dependencies use it instead of other options.

And to work with those frameworks and dependencies, you'll need to convert your internal numeric representations to BigDecimal.

How to Convert String to BigDecimal

Let's start with how to convert a String to a BigDecimal.

It's simple: use the constructor. Try this on for size:

final BigDecimal bd = new BigDecimal("123.456789");
System.err.println(bd);

That will print out exactly what you'd expect to see.

There are other ways to accomplish the same thing, but they're roundabout and won't give you the level of concision you're looking for in your code.

So I'd stick with the constructor method.

How to Convert Integer to BigDecimal

Yep. Sometimes you'll need to convert Integer to BigDecimal. I've been there a few times myself.

Once again, just use the contructor.

final BigDecimal bd = new BigDecimal(7);
System.err.println(bd);

That's going to give you a red 7.

But you could also use the valueOf() static method. Here's how it works:

final Integer i = 7;
final BigDecimal bd = BigDecimal.valueOf(i);
System.err.println(bd);

And that will print out a nice red 7.

Now if you're an astute observer of the Javadocs, you might wonder how that works. Cuz there ain't no valueOf() method that accepts an int parameter.

Answer: the JVM just translates it to a long on the fly. So it's all good.

How to Convert Double or Float to BigDecimal

This time: don't use the constructor.

Go with valueOf().

If you go with the constructor method but don't specify rounding, you could get a number that's ever-so-slightly different from the number you specified.

Take a look at this example:

final double d = 63.54d;
final BigDecimal bd = new BigDecimal(d);
System.err.println(bd);

That gives me this output:

63.53999999999999914734871708787977695465087890625

Not quite what I had in mind.

But if I go this route:

final double d = 63.54d;
final BigDecimal bd = BigDecimal.valueOf(d);
System.err.println(bd);

That gives me this output:

63.54

Much better.

According to the JavaDoc: "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)."

Wrapping It Up

Now you now how to convert a String and various numeric types to BigDecimal.

I think you'll find you need to do so more often than you'd like.

But at least you know how.

Have fun!

Photo by Magda Ehlers from Pexels