org.jopendocument.util
Class DecimalUtils

java.lang.Object
  extended by org.jopendocument.util.DecimalUtils

public class DecimalUtils
extends Object


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

HIGH_PRECISION

public static final MathContext HIGH_PRECISION
Useful to pass to BigDecimal.divide(BigDecimal, MathContext) since : 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 Detail

DecimalUtils

public DecimalUtils()
Method Detail

intDigits

public static int intDigits(BigDecimal d)
The number of int digits.

Parameters:
d - the decimal to use, eg "123.45".
Returns:
number of int digits, never less than 1, eg 3.

decimalDigits

public static int decimalDigits(BigDecimal d)
The number of decimal digits.

Parameters:
d - the decimal to use, eg "123.45" or "120".
Returns:
number of decimal digits, never less than 0, eg 2 or 0.

round

public static BigDecimal round(BigDecimal d,
                               int decimalDigits)

round

public static BigDecimal round(BigDecimal d,
                               int decimalDigits,
                               RoundingMode rmode)
Round the passed BigDecimal so that only decimalDigits remain after the point.

Parameters:
d - the decimal to use, eg "123.4567".
decimalDigits - the number of digits after the point, eg 1.
rmode - how to round.
Returns:
d rounded so that only decimalDigits remain, eg "123.5".


Copyright © 2010 jOpenDocument All Rights Reserved.