The Java class BigDec­i­mal makes it possible to process complex floating point numbers with precision. Once they’ve been created, you can apply different methods to them. The structure of the syntax is always the same, so it’s easy to quickly fa­mil­iar­ize yourself with the class.

What is Java BigDec­i­mal?

Java BigDec­i­mal allows you to ac­cu­rate­ly display and process complex floating point numbers of the­o­ret­i­cal­ly any size. This article will show you different methods for using this class, be it for rounding, arith­metic or format con­ver­sion. You’ll also learn how to implement it for hashing and for precise, so­phis­ti­cat­ed com­par­isons.

Java BigDec­i­mal consists of a 32-bit integer scale and an unscaled integer value with optional precision. In this case, “scale” means the number of digits after the decimal point, provided they’re greater than or equal to zero. However, if the value is less than zero, it is mul­ti­plied by 10^(-scale). The size of the class is limited only by the computer’s memory. Though this is more of a the­o­ret­i­cal con­sid­er­a­tion, as it’s unlikely that a program will create a number that exceeds its available memory. BigDec­i­mal in Java is intended ex­clu­sive­ly for floating point numbers, while the Big­In­te­ger class is used for pro­cess­ing integers.

Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

What is the class needed for?

Java BigDec­i­mal’s level of precision isn’t needed for every scenario. But there are sit­u­a­tions where its precision is in­valu­able. For example, it serves its purpose well in e-commerce trans­ac­tions, where cal­cu­la­tions can be impacted by even the smallest decimal place. The class is also used to conduct precise static analyses. Programs used for the control and nav­i­ga­tion of airplanes or rockets rely on the class, as does the medical segment. In other fields, the levels of precision offered by Java BigDec­i­mal provides the best possible security.

How is an object created?

To use BigDec­i­mal in Java, you’ll need to first import the class into your Java program. Once you’ve done that, you can declare an object of this class. Next, create the desired value as an argument and pass this to the ap­pro­pri­ate Java con­struc­tor. Once you’ve completed this process, you can use BigDec­i­mals in Java. Within the class, you’ll find various methods, which we’ll explain in more detail in the following section. First, we’re going to import the class and declare two BigDec­i­mal objects:

/ / Your Java program for the BigDecimal class
import java.math.BigDecimal;
public class BigDecimalExample
{
	public static void main(String[] args)
	{
		/ / Create two new BigDecimals
		BigDecimal ExampleOne =
			new BigDecimal ("1275936001.744297361");
		BigDecimal ExampleTwo =
			new BigDecimal ("4746691047.132719503");
	}
}
java

Now you can use these objects with the methods for the BigDec­i­mal class.

Examples for Java BigDec­i­mal

Once you have created the objects, you can use different methods to use the objects and perform op­er­a­tions. Let’s look at a few examples to show you how this works. The output is initiated using the Java command System.out.println().

Adding two BigDec­i­mals

If you want to add two BigDec­i­mals in Java, you need to use the add() method. To do this, store the two values that you want to calculate the sum for. In our example, the value ExampleOne will be added to the value ExampleTwo.

ExampleOne =
ExampleOne.add(ExampleTwo);
System.out.println ("Here is the result: " + ExampleOne);
java

Subtract numbers

To subtract two values from each other, you need the subtract() method. In the next example, we subtract ExampleTwo from ExampleOne.

ExampleOne =
ExampleOne.subtract(ExampleTwo);
System.out.println ("Here is the result: " + ExampleOne);
java

Multiply values

The method you use to multiply two BigDec­i­mals in Java works similarly. It’s called multiply(). To multiply ExampleTwo by ExampleOne, use the following code:

ExampleOne =
ExampleOne.multiply(ExampleTwo);
System.out.println ("Here is the result: " + ExampleOne);
java

Dividing numbers

If you want to divide two BigDec­i­mal objects in Java, use the divide() method. This follows the same syntax as the other examples and looks like this:

ExampleOne =
ExampleOne.divide(ExampleTwo);
System.out.println ("Here is the result: " + ExampleOne);
java

However, this only works if the result is exact or an integer. If this is not the case, the following error message will be output: java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.. This describes a runtime error. To avoid this, there are various rounding options for the divide() method, which can be trans­mit­ted via java.math.Round­ing­Mode. You can choose from the following constants:

Constant Function
CEILING Rounds to positive infinity
DOWN Rounds to 0
FLOOR Rounds to negative infinity
HALF_DOWN Rounds to the nearest neigh­bor­ing number and the opposite of 0 if both are equidis­tant
HALF_EVEN Rounds to the next neigh­bor­ing number and to the next even number if both are equidis­tant
HALF_UP Rounds to the nearest neigh­bor­ing number and in the direction of 0, provided they are both the same distance away
UN­NEC­ES­SARY Omits rounding and only performs exact op­er­a­tions; can only be used if the division is exact
UP Rounds away from 0

Overview of the most important methods

Now that you’ve learned how to use BigDec­i­mal in Java, here’s an overview of some of the most important methods you can use with it.

Method Function
abs() Returns a BigDec­i­mal with its absolute value
add() Returns a BigDec­i­mal whose value is composed of (this + Addend)
divide() Output value results from (this / Divisor)
max(BigDec­i­mal val) Outputs the maximum of the BigDec­i­mal
min(BigDec­i­mal val) Outputs the minimum of the BigDec­i­mal
move­PointLeft(int n) Outputs a BigDec­i­mal where the decimal point has been moved to the left by the value “n”
move­PointRight(int n) Outputs a BigDec­i­mal where the decimal point has been moved to the right by the value “n”
multiply(BigDec­i­mal mul­ti­pli­cand, Math­Con­text mc) Returns a value that results from (this * mul­ti­pli­cand)
Tip

In our Digital Guide, you can learn more about Java. You can read about Java operators, the dif­fer­ences between Java and JavaScript or find out how Java and Python compare.

Go to Main Menu