Javatpoint Logo
Javatpoint Logo

Java BigDecimal class

The BigDecimal class provides operation for arithmetic, comparison, hashing, rounding, manipulation and format conversion. This method can handle very small and very big floating point numbers with great precision.

In java, BigDecimal consists of a random precision integer scale and a 32-bit integer scale. If positive or zero, the scale is the number of digits to the right of the decimal point. If less than zero, the unscaled value of the number is multiplied by ten to the power of the negation of the scale(10^(-scale)).

Example 1

Test it Now

Output:

Returned int value is = -36755
Returned Exact Integer Value of 63745 is = 63745

Example 2

Test it Now

Output:

Max Value among 152207 and 179311 is = 179311
Min Value among 152207 and 128114 is = 128114

Java BigDecimal methods

SN Modifier and Type Method Description
1 BigDecimal abs() This method returns a BigDecimal whose value is the absolute value of this BigDecimal and whose scale is this.scale().
2 BigDecimal add() This method returns a BigDecimal whose value is (this + augend) and whose scale is max(this.scale(), augend.scale()).
3 byte byteValueExact() This method is used to convert the BigDecimal to a byte and check for lost information.
4 int compareTo() This method compares this BigDecimal with the specified BigDecimal.
5 BigDecimal divide() This method returns a BigDecimal whose value is (this / divisor) and whose preferred scale is (this.scale() - divisor.scale()).
6 BigDecimal[] divideAndRemainder() This method is used to return a two-element BigDecimal array which contains the result of divideToIntegralValue followed by the result of remainder on the two operands.
7 BigDecimal divideToIntegralValue() This method is used to return a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.
8 double doubleValue() This method is used to convert this BigDecimal to a double.
9 boolean equals() This method is used to compare the BigDecimal with the specified Object for equality.
10 float floatValue() This method is used to convert this BigDecimal to a float.
11 int hashCode() This method returns the hash code for this BigDecimal.
12 int intValue() This method is sued to convert this BigDecimal to an int.
13 int intValueExact() This method is used to convert this BigDecimal to an int, checking for lost information.
14 long longValue() This method converts this BigDecimal to a long.
15 long longValueExact() This method is used to convert this BigDecimal to a long, checking for lost information.
16 BigDecimal max() This method returns the maximum of this BigDecimal and val.
17 BigDecimal min() This method returns the minimum of this BigDecimal and val.
18 BigDecimal movePointLeft() This method returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.
19 BigDecimal movePointRight() This method is used to return a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.
20 BigDecimal multiply() This method returns a BigDecimal whose value is (this multiplicand), and whose scale is (this.scale() + multiplicand.scale())
21 BigDecimal negate() This method returns a BigDecimal whose value is (-this), and whose scale is this.scale().
22 BigDecimal plus() This method returns a BigDecimal whose value is (+this), and whose scale is this.scale().
23 BigDecimal pow() This method returns a BigDecimal whose value is (thisn),
24 int precision() This method returns the precision of this BigDecimal.
25 BigDecimal remainder() This method returns a BigDecimal whose value is (this % divisor).
26 BigDecimal round() This method returns a BigDecimal rounded according to the MathContext settings.
27 int scale() This method returns the scale of this BigDecimal.
28 BigDecimal scaleByPowerOfTen() This method returns a BigDecimal whose numerical value is equal to (this * 10n).
29 short shortValueExact() This method is used to convert this BigDecimal to a short, checking for lost information.
30 int signum() This method returns the signum function of this BigDecimal.
31 BigDecimal stripTrailingZeros() This method returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.
32 BigDecimal Subtract() This method returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale())
33 BigInteger toBigInteger() This method is used to convert this BigDecimal to a BigInteger.
34 BigInteger toBigIntegerExact() This method is used to convert this BigDecimal to a BigInteger, checking for lost information.
35 String toEngineeringString() This method returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.
36 String toString() This method returns the string representation of this BigDecimal
37 BigDecimal ulp() This method returns the size of an ulp, a unit in the last place, of this BigDecimal.
38 static BigDecimal valueOf() This method is used to translate a double into a BigDecimal.

Next TopicJava BigDecimal





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA