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




Join the conversation! Your thoughts help the community grow.