BigDecimal class

                                 
    java.lang.Object                            
     |                           
     |--java.lang.Number                        
         |                       
         |--java.math.BigDecimal                    
                                 

BigDecimal represnet immutable, arbitrary-precision signed decimal numbers.

Constants for One, Ten and Zero

TypeFieldSummary
static BigDecimalONEThe value 1, with a scale of 0.
static BigDecimalTENThe value 10, with a scale of 0.
static BigDecimalZEROThe value 0, with a scale of 0.

Rounding mode

TypeFieldSummary
static intROUND_CEILINGRound towards positive infinity.
static intROUND_DOWNRound towards zero.
static intROUND_FLOORRound towards negative infinity.
static intROUND_HALF_DOWNRound towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
static intROUND_HALF_EVENRound towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
static intROUND_HALF_UPRound towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
static intROUND_UNNECESSARYNo rounding is necessary.
static intROUND_UPRounding mode to round away from zero.

ConstructorSummary
BigDecimal(BigInteger val)Converts a BigInteger into a BigDecimal.
BigDecimal(BigInteger unscaledVal, int scale)Converts a BigInteger with scale into a BigDecimal.
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)Converts a BigInteger and an int scale into a BigDecimal, with rounding according to the context settings.
BigDecimal(BigInteger val, MathContext mc)Converts a BigInteger into a BigDecimal rounding according to the context settings.
BigDecimal(char[] in)Converts a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor.
BigDecimal(char[] in, int offset, int len)Converts a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified.
BigDecimal(char[] in, int offset, int len, MathContext mc)Converts a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified and with rounding according to the context settings.
BigDecimal(char[] in, MathContext mc)Converts a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor and with rounding according to the context settings.
BigDecimal(double val)Converts a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value.
BigDecimal(double val, MathContext mc)Converts a double into a BigDecimal, with rounding according to the context settings.
BigDecimal(int val)Converts an int into a BigDecimal.
BigDecimal(int val, MathContext mc)Converts an int into a BigDecimal, with rounding according to the context settings.
BigDecimal(long val)Converts a long into a BigDecimal.
BigDecimal(long val, MathContext mc)Converts a long into a BigDecimal, with rounding according to the context settings.
BigDecimal(String val)Converts the string representation of a BigDecimal into a BigDecimal.
BigDecimal(String val, MathContext mc)Converts the string representation of a BigDecimal into a BigDecimal, accepting the same strings as the BigDecimal(String) constructor, with rounding according to the context settings.

Methods used to do calculation

ReturnMethod
BigDecimalabs()
BigDecimalabs(MathContext mc)
BigDecimaladd(BigDecimal augend)
BigDecimaladd(BigDecimal augend, MathContext mc)
BigDecimaldivide(BigDecimal divisor)
BigDecimaldivide(BigDecimal divisor, int roundingMode)
BigDecimaldivide(BigDecimal divisor, int scale, int roundingMode)
BigDecimaldivide(BigDecimal divisor, int scale, RoundingMode roundingMode)
BigDecimaldivide(BigDecimal divisor, MathContext mc)
BigDecimaldivide(BigDecimal divisor, RoundingMode roundingMode)
BigDecimal[]divideAndRemainder(BigDecimal divisor)
BigDecimal[]divideAndRemainder(BigDecimal divisor, MathContext mc)
BigDecimaldivideToIntegralValue(BigDecimal divisor)
BigDecimaldivideToIntegralValue(BigDecimal divisor, MathContext mc)
BigDecimalmax(BigDecimal val)
BigDecimalmin(BigDecimal val)
BigDecimalmultiply(BigDecimal multiplicand)
BigDecimalmultiply(BigDecimal multiplicand, MathContext mc)
BigDecimalnegate()
BigDecimalnegate(MathContext mc)
BigDecimalplus()
BigDecimalplus(MathContext mc)
BigDecimalpow(int n)
BigDecimalpow(int n, MathContext mc)
BigDecimalremainder(BigDecimal divisor)
BigDecimalremainder(BigDecimal divisor, MathContext mc)
BigDecimalround(MathContext mc)
BigDecimalscaleByPowerOfTen(int n)
BigDecimalsubtract(BigDecimal subtrahend)
BigDecimalsubtract(BigDecimal subtrahend, MathContext mc)

Convert BigDecimal to primitive data types

ReturnMethodSummary
bytebyteValueExact()Converts this BigDecimal to a byte, checking for lost information.
doubledoubleValue()Converts this BigDecimal to a double.
floatfloatValue()Converts this BigDecimal to a float.
intintValue()Converts this BigDecimal to an int.
intintValueExact()Converts this BigDecimal to an int, checking for lost information.
longlongValue()Converts this BigDecimal to a long.
longlongValueExact()Converts this BigDecimal to a long, checking for lost information.
shortshortValueExact()Converts this BigDecimal to a short, checking for lost information.
BigIntegertoBigInteger()Converts this BigDecimal to a BigInteger.
BigIntegertoBigIntegerExact()Converts this BigDecimal to a BigInteger, checking for lost information.

Compare two BigDecimal

ReturnMethodSummary
intcompareTo(BigDecimal val)Compares this BigDecimal with the specified BigDecimal.
booleanequals(Object x)Compares this BigDecimal with the specified Object for equality.

Move decimal point

ReturnMethodSummary
BigDecimalmovePointLeft(int n)decimal point moved n places to the left.
BigDecimalmovePointRight(int n)decimal point moved n places to the right.

Scale and precision

ReturnMethodSummary
intprecision()Returns the precision.
intscale()Returns the scale.
BigDecimalsetScale(int newScale)Change the scale.
BigDecimalsetScale(int newScale, int roundingMode)Set scale with rounding Mode.
BigDecimalsetScale(int newScale, RoundingMode roundingMode)Set scale with rounding Mode.
intsignum()Returns the signum function of this BigDecimal.
BigIntegerunscaledValue()Get the unscaled value.
BigDecimalulp()Returns the size of an ulp(a unit in the last place).

Convert BigDecimal to String

ReturnMethodSummary
StringtoEngineeringString()Using engineering notation if an exponent is needed.
StringtoPlainString()Without an exponent field.
StringtoString()Using scientific notation if an exponent is needed.

Remove the trailing zeros

ReturnMethodSummary
BigDecimalstripTrailingZeros()trailing zeros removed.

Convert double and long to BigDecimal

ReturnMethodSummary
static BigDecimalvalueOf(double val)Translates a double into a BigDecimal.
static BigDecimalvalueOf(long val)Translates a long value into a BigDecimal with a scale of zero.
static BigDecimalvalueOf(long unscaledVal, int scale)Translates a long unscaled value and an int scale into a BigDecimal.
Revised from Open JDK source code
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.