Search in sources :

Example 1 with MathContext

use of java.math.MathContext in project groovy by apache.

the class BigDecimalMath method divideImpl.

public Number divideImpl(Number left, Number right) {
    BigDecimal bigLeft = toBigDecimal(left);
    BigDecimal bigRight = toBigDecimal(right);
    try {
        return bigLeft.divide(bigRight);
    } catch (ArithmeticException e) {
        // set a DEFAULT precision if otherwise non-terminating
        int precision = Math.max(bigLeft.precision(), bigRight.precision()) + DIVISION_EXTRA_PRECISION;
        BigDecimal result = bigLeft.divide(bigRight, new MathContext(precision));
        int scale = Math.max(Math.max(bigLeft.scale(), bigRight.scale()), DIVISION_MIN_SCALE);
        if (result.scale() > scale)
            result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return result;
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 2 with MathContext

use of java.math.MathContext in project head by mifos.

the class Money method round.

     * This method returns a new Money object with currency same as current
     * currency and amount calculated after rounding based on rounding mode and
     * roundingAmount where in both are obtained from MifosCurrency object. <br />
     * <br />
     * The rounding calculation is as follows:- Lets say we want to round 142.34
     * to nearest 50 cents and and rounding mode is ceil (i.e. to greater
     * number) we will divide 142.34 by .5 which will result in 284.68 now we
     * will round this to a whole number using ceil mode which will result in
     * 285 and then multiply 285 by 0.5 resulting in 142.5.
public static Money round(Money money, BigDecimal roundOffMultiple, RoundingMode roundingMode) {
    // insure that we are using the correct internal precision
    BigDecimal roundingAmount = roundOffMultiple.setScale(internalPrecision, internalRoundingMode);
    // FIXME: are we loosing precision here
    // mathcontext only take cares of significant digits
    // not digit right to the decimal
    BigDecimal nearestFactor = money.getAmount().divide(roundingAmount, new MathContext(internalPrecision, internalRoundingMode));
    nearestFactor = nearestFactor.setScale(0, roundingMode);
    BigDecimal roundedAmount = nearestFactor.multiply(roundingAmount);
    return new Money(money.getCurrency(), roundedAmount);
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 3 with MathContext

use of java.math.MathContext in project openhab1-addons by openhab.

the class PlexConnector method getProgressCommand.

private String getProgressCommand(PlexBindingConfig config, Command command) {
    PlexSession session = getSessionByMachineId(config.getMachineIdentifier());
    String url = null;
    if (session != null) {
        int offset = 0;
        if (command.getClass().equals(PercentType.class)) {
            PercentType percent = (PercentType) command;
            offset = new BigDecimal(session.getDuration()).multiply(percent.toBigDecimal().divide(new BigDecimal("100"), new MathContext(5, RoundingMode.HALF_UP))).intValue();
            offset = Math.max(0, offset);
            offset = Math.min(session.getDuration(), offset);
            url = String.format("playback/seekTo?offset=%d", offset);
        } else if (command.getClass().equals(IncreaseDecreaseType.class)) {
            if (command.equals(IncreaseDecreaseType.DECREASE)) {
                url = PlexProperty.STEP_BACK.getName();
            } else {
                url = PlexProperty.STEP_FORWARD.getName();
    return url;
Also used : IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) PercentType(org.openhab.core.library.types.PercentType) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 4 with MathContext

use of java.math.MathContext in project openhab1-addons by openhab.

the class PlexSession method updateProgress.

private void updateProgress() {
    if (duration > 0) {
        BigDecimal progress = new BigDecimal("100").divide(new BigDecimal(duration), new MathContext(100, RoundingMode.HALF_UP)).multiply(new BigDecimal(viewOffset)).setScale(2, RoundingMode.HALF_UP);
        progress = BigDecimal.ZERO.max(progress);
        progress = new BigDecimal("100").min(progress);
        this.progress = progress;
        this.endTime = new Date(System.currentTimeMillis() + (duration - viewOffset));
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Date(java.util.Date)

Example 5 with MathContext

use of java.math.MathContext in project robovm by robovm.

the class BigDecimalConstructorsTest method testConstrLongMathContext.

     * new BigDecimal(long, MathContext)
public void testConstrLongMathContext() {
    long a = 4576578677732546982L;
    int precision = 5;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    String res = "45766";
    int resScale = -14;
    BigDecimal result = new BigDecimal(a, mc);
    assertEquals("incorrect value", res, result.unscaledValue().toString());
    assertEquals("incorrect scale", resScale, result.scale());
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)


MathContext (java.math.MathContext)141 BigDecimal (java.math.BigDecimal)138 RoundingMode (java.math.RoundingMode)72 BigInteger (java.math.BigInteger)70 Date (java.util.Date)2 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)2 Well19937c (org.apache.commons.math3.random.Well19937c)2 SideOnly (cpw.mods.fml.relauncher.SideOnly)1 NumberFormat (java.text.NumberFormat)1 ParseException (java.text.ParseException)1 Formatter (java.util.Formatter)1 Alert (javafx.scene.control.Alert)1 ResponseOptionBean (org.akaza.openclinica.bean.submit.ResponseOptionBean)1 OpenClinicaException (org.akaza.openclinica.exception.OpenClinicaException)1 ScoreException (org.akaza.openclinica.exception.ScoreException)1 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)1 SappBindingConfigDimmerItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem)1 SappBindingConfigNumberItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem)1 SappBindingConfigRollershutterItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem)1 SappBindingConfigSwitchItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem)1