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

SNModifier and TypeMethodDescription
1BigDecimalabs()This method returns a BigDecimal whose value is the absolute value of this BigDecimal and whose scale is this.scale().
2BigDecimaladd()This method returns a BigDecimal whose value is (this + augend) and whose scale is max(this.scale(), augend.scale()).
3bytebyteValueExact()This method is used to convert the BigDecimal to a byte and check for lost information.
4intcompareTo()This method compares this BigDecimal with the specified BigDecimal.
5BigDecimaldivide()This method returns a BigDecimal whose value is (this / divisor) and whose preferred scale is (this.scale() - divisor.scale()).
6BigDecimal[]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.
7BigDecimaldivideToIntegralValue()This method is used to return a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.
8doubledoubleValue()This method is used to convert this BigDecimal to a double.
9booleanequals()This method is used to compare the BigDecimal with the specified Object for equality.
10floatfloatValue()This method is used to convert this BigDecimal to a float.
11inthashCode()This method returns the hash code for this BigDecimal.
12intintValue()This method is sued to convert this BigDecimal to an int.
13intintValueExact()This method is used to convert this BigDecimal to an int, checking for lost information.
14longlongValue()This method converts this BigDecimal to a long.
15longlongValueExact()This method is used to convert this BigDecimal to a long, checking for lost information.
16BigDecimalmax()This method returns the maximum of this BigDecimal and val.
17BigDecimalmin()This method returns the minimum of this BigDecimal and val.
18BigDecimalmovePointLeft()This method returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.
19BigDecimalmovePointRight()This method is used to return a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.
20BigDecimalmultiply()This method returns a BigDecimal whose value is (this � multiplicand), and whose scale is (this.scale() + multiplicand.scale())
21BigDecimalnegate()This method returns a BigDecimal whose value is (-this), and whose scale is this.scale().
22BigDecimalplus()This method returns a BigDecimal whose value is (+this), and whose scale is this.scale().
23BigDecimalpow()This method returns a BigDecimal whose value is (thisn),
24intprecision()This method returns the precision of this BigDecimal.
25BigDecimalremainder()This method returns a BigDecimal whose value is (this % divisor).
26BigDecimalround()This method returns a BigDecimal rounded according to the MathContext settings.
27intscale()This method returns the scale of this BigDecimal.
28BigDecimalscaleByPowerOfTen()This method returns a BigDecimal whose numerical value is equal to (this * 10n).
29shortshortValueExact()This method is used to convert this BigDecimal to a short, checking for lost information.
30intsignum()This method returns the signum function of this BigDecimal.
31BigDecimalstripTrailingZeros()This method returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.
32BigDecimalSubtract()This method returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale())
33BigIntegertoBigInteger()This method is used to convert this BigDecimal to a BigInteger.
34BigIntegertoBigIntegerExact()This method is used to convert this BigDecimal to a BigInteger, checking for lost information.
35StringtoEngineeringString()This method returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.
36StringtoString()This method returns the string representation of this BigDecimal
37BigDecimalulp()This method returns the size of an ulp, a unit in the last place, of this BigDecimal.
38static BigDecimalvalueOf()This method is used to translate a double into a BigDecimal.

Next TopicJava BigDecimal




Latest Courses