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
Some Examples of BigDecimal Class
BigDecimal represnet 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
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
import java.math.BigDecimal;
public class Create
{
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
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:
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:
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.