Search in sources :

Example 21 with MathContext

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

the class BigDecimalConstructorsTest method testConstrIntMathContext.

/**
     * new BigDecimal(int, MathContext)
     */
public void testConstrIntMathContext() {
    int a = 732546982;
    int precision = 21;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    String res = "732546982";
    int resScale = 0;
    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)

Example 22 with MathContext

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

the class BigDecimalConstructorsTest method testConstrCharIntIntMathContext.

/**
     * new BigDecimal(char[] value, int offset, int len, MathContext mc); 
     */
public void testConstrCharIntIntMathContext() {
    char[] value = { '-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3' };
    int offset = 3;
    int len = 12;
    int precision = 4;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    BigDecimal result = new BigDecimal(value, offset, len, mc);
    String res = "3.805E-40";
    int resScale = 43;
    assertEquals("incorrect value", res, result.toString());
    assertEquals("incorrect scale", resScale, result.scale());
    try {
        // Regression for HARMONY-783
        new BigDecimal(new char[] {}, 0, 0, MathContext.DECIMAL32);
        fail("NumberFormatException has not been thrown");
    } catch (NumberFormatException e) {
    }
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 23 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 24 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 25 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)

Aggregations

MathContext (java.math.MathContext)237 BigDecimal (java.math.BigDecimal)224 RoundingMode (java.math.RoundingMode)73 BigInteger (java.math.BigInteger)71 List (java.util.List)9 Test (org.junit.Test)8 BigDecimalMath (ch.obermuhlner.math.big.BigDecimalMath)7 Arrays (java.util.Arrays)7 Function (java.util.function.Function)7 StopWatch (ch.obermuhlner.math.big.example.StopWatch)3 IOException (java.io.IOException)3 Random (java.util.Random)3 Context (ch.obermuhlner.math.big.BigFloat.Context)2 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)2 Well19937c (org.apache.commons.math3.random.Well19937c)2 DataCell (org.knime.core.data.DataCell)2