The Java class Big­Decim­al 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­i­ar­ise yourself with the class.

What is Java Big­Decim­al?

Java Big­Decim­al allows you to ac­cur­ately display and process complex floating point numbers of the­or­et­ic­ally any size. This article will show you different methods for using this class, be it for rounding, arith­met­ic or format con­ver­sion. You’ll also learn how to implement it for hashing and for precise, soph­ist­ic­ated com­par­is­ons.

Java Big­Decim­al 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­or­et­ic­al con­sid­er­a­tion, as it’s unlikely that a program will create a number that exceeds its available memory. Big­Decim­al in Java is intended ex­clus­ively for floating point numbers, while the Bi­gIn­teger class is used for pro­cessing integers.

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

What is the class needed for?

Java Big­Decim­al’s level of precision isn’t needed for every scenario. But there are situ­ations 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­ig­a­tion of aero­planes or rockets rely on the class, as does the medical segment. In other fields, the levels of precision offered by Java Big­Decim­al provides the best possible security.

How is an object created?

To use Big­Decim­al 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­struct­or. Once you’ve completed this process, you can use Big­Decim­als 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 Big­Decim­al 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 Big­Decim­al class.

Examples for Java Big­Decim­al

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 Big­Decim­als

If you want to add two Big­Decim­als 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 Big­Decim­als 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 Big­Decim­al 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­bour­ing number and the opposite of 0 if both are equidistant
HALF_EVEN Rounds to the next neigh­bour­ing number and to the next even number if both are equidistant
HALF_UP Rounds to the nearest neigh­bour­ing number and in the direction of 0, provided they are both the same distance away
UN­NE­CES­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 Big­Decim­al in Java, here’s an overview of some of the most important methods you can use with it.

Method Function
abs() Returns a Big­Decim­al with its absolute value
add() Returns a Big­Decim­al whose value is composed of (this + Addend)
divide() Output value results from (this / Divisor)
max(Big­Decim­al val) Outputs the maximum of the Big­Decim­al
min(Big­Decim­al val) Outputs the minimum of the Big­Decim­al
move­Poin­tLeft(int n) Outputs a Big­Decim­al where the decimal point has been moved to the left by the value ‘n’
move­PointRight(int n) Outputs a Big­Decim­al where the decimal point has been moved to the right by the value ‘n’
multiply(Big­Decim­al mul­ti­plic­and, Math­Con­text mc) Returns a value that results from (this * mul­ti­plic­and)
Tip

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

47acc86d894d22361ce62e045b3f198d

7a13b71b544a0a47b82b89c94cb2cba2

a043a1b67eddde60f587280a4ca1a324

bc7ff3cc8bd2c8e909d817f10f496251

388d113eebff89cedf1ec961d226da55

88e50c15131295c585c6b5056783b96e

Go to Main Menu