Java number to bigdecimal

Convert Integer to BigDecimal in Java [2 ways]

You can easily use BigDecimal’s valueOf() method to convert Integer to BigDecimal.

BigDecimal. valueOf ( int );
Below is the complete example to convert Integer and primitive int to BigDecimal in Java using the valueOf() method.

import java.math.BigDecimal; public class IntegerToBigDecimalOne < public static void main(String args[]) < // Converting int to BigDecimal System.out.println("Converting int to BigDecimal"); int num = 100; BigDecimal bd = BigDecimal.valueOf(num); System.out.println(bd); // Converting Integer to BigDecimal System.out.println("Converting Integer to BigDecimal"); Integer num2 = 200; BigDecimal bd2 = BigDecimal.valueOf(num2); System.out.println(bd2); > > 

When you run the above program, you will get the below Output:
Converting int to BigDecimal
100
Converting Integer to BigDecimal
200

2. Using Constructor

Below is the complete example to convert primitive int and Integer to BigDecimal in Java using the constructor.

import java.math.BigDecimal; public class IntegerToBigDecimalTwo < public static void main(String args[]) < // Converting int to BigDecimal System.out.println("Converting int to BigDecimal"); int num = 300; BigDecimal bd3 = new BigDecimal(num); System.out.println(bd3); // Converting Integer to BigDecimal System.out.println("Converting Integer to BigDecimal"); Integer num2 = 400; BigDecimal bd4 = new BigDecimal(num2); System.out.println(bd4); > > 

The Output will be:
Converting int to BigDecimal
300
Converting Integer to BigDecimal
400

Читайте также:  Rubbish

That’s it for today, please mention in the comments in case you know any other way of converting Integer to BigDecimal in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry

Источник

Java: How to Convert Integer, String, Float, and Double to BigDecimal

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.

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.

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.

Читайте также:  Python файлы одного расширения

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.

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.

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

That gives me this output:

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) .»

Читайте также:  Cpp конструктор по умолчанию

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.

Источник

Оцените статью