|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object extended by org.jopendocument.util.DecimalUtils
public class DecimalUtils
| Field Summary | |
|---|---|
static MathContext |
HIGH_PRECISION
Useful to pass to BigDecimal.divide(BigDecimal, MathContext) since :
1234,567.divide(33000) throws java.lang.ArithmeticException: Non-terminating decimal
expansion; no exact representable decimal result
1234,567.divide(33000, RoundingMode.HALF_UP) = 0,037 : since it isn't exact uses the
dividend scale, thus loosing a lot of precision
1234,567.divide(33000, HIGH_PRECISION) = 0.03741112121212121212121212121212121
You also can't just use the precision of the dividend :
lowPrecision = new BigDecimal("1E+3");
lowPrecision.divide(new BigDecimal("7E+5"), new MathContext(lowPrecision.precision()))) == 0.001;
lowPrecision.divide(new BigDecimal("7E+5"), HIGH_PRECISION) == 0.001428571428571428571428571428571429;
Note: MathContext.DECIMAL128 is different since it uses
RoundingMode.HALF_EVEN
|
| Constructor Summary | |
|---|---|
DecimalUtils()
|
|
| Method Summary | |
|---|---|
static int |
decimalDigits(BigDecimal d)
The number of decimal digits. |
static int |
intDigits(BigDecimal d)
The number of int digits. |
static BigDecimal |
round(BigDecimal d,
int decimalDigits)
|
static BigDecimal |
round(BigDecimal d,
int decimalDigits,
RoundingMode rmode)
Round the passed BigDecimal so that only decimalDigits remain after the point. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final MathContext HIGH_PRECISION
BigDecimal.divide(BigDecimal, MathContext) since :
lowPrecision = new BigDecimal("1E+3");
lowPrecision.divide(new BigDecimal("7E+5"), new MathContext(lowPrecision.precision()))) == 0.001;
lowPrecision.divide(new BigDecimal("7E+5"), HIGH_PRECISION) == 0.001428571428571428571428571428571429;
Note: MathContext.DECIMAL128 is different since it uses
RoundingMode.HALF_EVEN
| Constructor Detail |
|---|
public DecimalUtils()
| Method Detail |
|---|
public static int intDigits(BigDecimal d)
d - the decimal to use, eg "123.45".
public static int decimalDigits(BigDecimal d)
d - the decimal to use, eg "123.45" or "120".
public static BigDecimal round(BigDecimal d, int decimalDigits)
public static BigDecimal round(BigDecimal d, int decimalDigits, RoundingMode rmode)
decimalDigits remain after the point.
d - the decimal to use, eg "123.4567".decimalDigits - the number of digits after the point, eg 1.rmode - how to round.
decimalDigits remain, eg "123.5".
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||