Example usage for java.math BigDecimal ROUND_HALF_EVEN

List of usage examples for java.math BigDecimal ROUND_HALF_EVEN

Introduction

In this page you can find the example usage for java.math BigDecimal ROUND_HALF_EVEN.

Prototype

int ROUND_HALF_EVEN

To view the source code for java.math BigDecimal ROUND_HALF_EVEN.

Click Source Link

Document

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("40");

    BigDecimal bg2 = new BigDecimal("3");

    BigDecimal bg3 = bg1.divide(bg2, BigDecimal.ROUND_HALF_EVEN);

    System.out.println(bg3);/* www  .  j  a  va2s  .  c  o  m*/
}

From source file:Main.java

/**
 * Compute x^exponent to a given scale. Uses the same algorithm as class
 * numbercruncher.mathutils.IntPower.//from www.j av  a 2 s.  c o  m
 * 
 * @param x
 *            the value x
 * @param exponent
 *            the exponent value
 * @param scale
 *            the desired scale of the result
 * @return the result value
 */
public static BigDecimal intPower(BigDecimal x, long exponent, int scale) {
    // If the exponent is negative, compute 1/(x^-exponent).
    if (exponent < 0) {
        return BigDecimal.valueOf(1).divide(intPower(x, -exponent, scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    BigDecimal power = BigDecimal.valueOf(1);

    // Loop to compute value^exponent.
    while (exponent > 0) {

        // Is the rightmost bit a 1?
        if ((exponent & 1) == 1) {
            power = power.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
        }

        // Square x and shift exponent 1 bit to the right.
        x = x.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
        exponent >>= 1;

        Thread.yield();
    }

    return power;
}

From source file:Main.java

/**
 * Compute e^x to a given scale.//from  w ww.j  a  v a  2 s.  co  m
 * Break x into its whole and fraction parts and
 * compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * @param x the value of x
 * @param scale the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0)
        return expTaylor(x, scale);

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:Main.java

/**
 * Compute e^x to a given scale. Break x into its whole and fraction parts
 * and compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * /*  w w  w. ja  v  a2  s  .co m*/
 * @param x
 *            the value of x
 * @param scale
 *            the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0) {
        return expTaylor(x, scale);
    }

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:com.hotelbeds.hotelapimodel.auto.convert.json.RateSerializer.java

@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeString(value.setScale(value.scale(), BigDecimal.ROUND_HALF_EVEN).toString());
}

From source file:org.datalorax.populace.core.populate.mutator.change.ChangeBigDecimalMutator.java

@Override
public Object mutate(final Type type, final Object currentValue, final Object parent,
        final PopulatorContext config) {
    Validate.isTrue(type.equals(BigDecimal.class), "BigDecimal type expected");
    if (currentValue == null) {
        return null;
    }/*from  ww w .j  a v a 2  s . c  o  m*/

    final BigDecimal bigDecimal = (BigDecimal) currentValue;
    return bigDecimal.divide(DIVISOR, BigDecimal.ROUND_HALF_EVEN);
}

From source file:Main.java

/**
 * Compute e^x to a given scale by the Taylor series.
 * @param x the value of x//  w w w .jav a2  s .  com
 * @param scale the desired scale of the result
 * @return the result value
 */
private static BigDecimal expTaylor(BigDecimal x, int scale) {
    BigDecimal factorial = BigDecimal.valueOf(1);
    BigDecimal xPower = x;
    BigDecimal sumPrev;

    // 1 + x
    BigDecimal sum = x.add(BigDecimal.valueOf(1));

    // Loop until the sums converge
    // (two successive sums are equal after rounding).
    int i = 2;
    do {
        // x^i
        xPower = xPower.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);

        // i!
        factorial = factorial.multiply(BigDecimal.valueOf(i));

        // x^i/i!
        BigDecimal term = xPower.divide(factorial, scale, BigDecimal.ROUND_HALF_EVEN);

        // sum = sum + x^i/i!
        sumPrev = sum;
        sum = sum.add(term);

        ++i;
        Thread.yield();
    } while (sum.compareTo(sumPrev) != 0);

    return sum;
}

From source file:cc.recommenders.mining.calls.NetworkMathUtils.java

@Deprecated
public static double round(final double value, final int precision) {
    return MathUtils.round(value, precision, BigDecimal.ROUND_HALF_EVEN);

}

From source file:Main.java

/**
 * Compute the natural logarithm of x to a given scale, x > 0.
 *///from   ww  w. j  a v  a  2  s  .  com
public static BigDecimal ln(BigDecimal x, int scale) {
    // Check that x > 0.
    if (x.signum() <= 0) {
        throw new IllegalArgumentException("x <= 0");
    }

    // The number of digits to the left of the decimal point.
    int magnitude = x.toString().length() - x.scale() - 1;

    if (magnitude < 3) {
        return lnNewton(x, scale);
    }

    // Compute magnitude*ln(x^(1/magnitude)).
    else {

        // x^(1/magnitude)
        BigDecimal root = intRoot(x, magnitude, scale);

        // ln(x^(1/magnitude))
        BigDecimal lnRoot = lnNewton(root, scale);

        // magnitude*ln(x^(1/magnitude))
        return BigDecimal.valueOf(magnitude).multiply(lnRoot).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
    }
}

From source file:org.hephaestus.fixedformat.impl.TestRecordFactoryBase.java

public void testDetail1Format() {
    TstDetailRecord tdr = new TstDetailRecord();
    tdr.setRecordType(2L);// www.j  av  a 2s  . co  m
    tdr.setDescription("Item 1");
    tdr.setAmount(new BigDecimal("3.03").setScale(2, BigDecimal.ROUND_HALF_EVEN));

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, Calendar.AUGUST);
    cal.set(Calendar.DAY_OF_MONTH, 31);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 33);
    tdr.setDate(cal.getTime());
    tdr.setTime(cal.getTime());

    assertEquals(DETAIL_RECORD_1, recordFactory.formatRecord(tdr));
}