BigDecimal Class
The BigDecimal class is used to manipulate and round floating point numbers. It helps the programmer to obtain accurate decimal results in business applications requiring a higher degree of accuracy. BigDecimal provides similar support for very large or very accurate
floating point numbers. Immutable, arbitrary-precision signed decimal numbers. A
BigDecimal consists of an arbitrary precision integer unscaled value and a
non-negative 32-bit integer scale, which represents the number of digits to the
right of the decimal point. The number represented by the BigDecimal.
BigDecimal provides operations for basic arithmetic, scale
manipulation, comparison, hashing, and format conversion. It is defined in the
library as:
java.lang.Object
extended byjava.lang.Number
extended byjava.math.BigDecimal
Examples of BigDecimal Class
BigDecimal represents immutable,
arbitrary-precision signed decimal numbers.
Constants for One, Ten and Zero.
Mainconstant.java
- import java.math.BigDecimal;
- public class Mainconstant
- {
- public static void main(String[] args) {
- System.out.println(BigDecimal.ONE);
- System.out.println(BigDecimal.TEN);
- System.out.println(BigDecimal.ZERO);
- }
- }
Output
Rounding Mode
The java.math.RoundingMode enumeration specifies a rounding behavior for numerical operations capable of discarding precision. Each rounding mode indicates how the least significant returned digit of a rounded result is to be calculated.
This Enum provides various constants each of which is used for different modes of rounding. The decimal value would be rounded off to the number of decimal places based on the scale that is defined for the decimal value.
java.lang.Object
java.lang.Enum<RoundingMode>
java.math.RoundingMode
Round.java
- import java.math.BigDecimal;
- public class Round
- {
- public static void main(String[] args)
- {
- BigDecimal first = new BigDecimal(1f);
- BigDecimal second = new BigDecimal(2f);
- BigDecimal result1 = first.divide(second, BigDecimal.ROUND_CEILING);
- BigDecimal result2 = first.divide(second, BigDecimal.ROUND_DOWN);
- BigDecimal result3 = first.divide(second, BigDecimal.ROUND_FLOOR);
- BigDecimal result4 = first.divide(second, BigDecimal.ROUND_HALF_DOWN);
- BigDecimal result5 = first.divide(second, BigDecimal.ROUND_HALF_EVEN);
- BigDecimal result6 = first.divide(second, BigDecimal.ROUND_HALF_UP);
- BigDecimal result7 = first.divide(second, BigDecimal.ROUND_UP);
- System.out.println(result1);
- System.out.println(result2);
- System.out.println(result3);
- System.out.println(result4);
- System.out.println(result5);
- System.out.println(result6);
- System.out.println(result7);
- }
- }
Output
Create BigDecimals
To create a BigDecimal object in Java, you call one of the constructors. Each of these constructors takes a value and converts it to a BigDecimal object.
CreateBigDecimal.java
- import java.math.BigDecimal;
- public class CreateBigDecimal
- {
- public static void main(String[] args)
- {
- System.out.println(new BigDecimal(1f));
- System.out.println(new BigDecimal(2f));
- System.out.println(new BigDecimal(3f));
- System.out.println(new BigDecimal(4f));
- System.out.println(new BigDecimal(5f));
- System.out.println(new BigDecimal(5f));
- System.out.println(new BigDecimal(6f));
- }
- }
Output
Method use For Calculation
It is method used for calculation in BigDecimal class in Java.
Method.java
- import java.math.BigDecimal;
-
- public class Method
- {
- public static void main(String[] args)
- {
- BigDecimal first = new BigDecimal(-5f);
- System.out.println(first);
- System.out.println(first.abs());
- }
- }
Output
Compare Two Bigdecimal
It is method used for comparing two BigDecimal objects in Java.
Compare.java
- import java.math.BigDecimal;
- public class Compare
- {
- public static void main(String[] args)
- {
- BigDecimal first = new BigDecimal(10f);
- BigDecimal second = new BigDecimal(10f);
- System.out.println(first.compareTo(second));
- BigDecimal third = new BigDecimal(10f);
- BigDecimal fourth = new BigDecimal(20f);
- System.out.println(third.compareTo(fourth));
- }
- }
Output
Move Decimal
This method is used to move the decimal point of the current BigDecimal by n places to the right.
Move.java
- import java.math.BigDecimal;
- public class Move
- {
- public static void main(String[] args)
- {
- BigDecimal first = new BigDecimal(10000f);
- System.out.println(first);
- System.out.println(first.movePointLeft(1));
- System.out.println(first.movePointLeft(2));
- System.out.println(first.movePointLeft(3));
- }
- }
Output
There are some another uses of Bigdecimal Class.
Remove the trailing zeroes,
Convert double into long decimal, Scale and precision.
Summary
In this article, we learned about BigDecimal class in Java and how to use them in our Java programs.