BigDecimal in Java

Udesh Kavinda
Oct 5, 2021

To create a BigDecimal object in Java, you call one of the constructors you see below. Each of these constructors takes a value and converts it to a BigDecimal object.

BigDecimal(String val)

BigDecimal(long val)

BigDecimal(int val)

BigDecimal(float val)

BigDecimal(double val)

BigDecimal is a class that belongs to java.math package. We use float and double primitive data types for decimal numbers but there is one problem with these primitive types. Float and double that these types should never be used for precise value, such as currency.

BigDecimal value = new BigDecimal(0.01);
System.out.println(value);
0.01000000000000000020816681711721685132943093776702880859375BigDecimal value = new BigDecimal("0.01");0.01

--

--

Udesh Kavinda
Udesh Kavinda

No responses yet