Java Code Examples for java.math.RoundingMode#HALF_EVEN

The following examples show how to use java.math.RoundingMode#HALF_EVEN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MonetaryRoundedFactoryBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnScaleRoundingOperator() {
	int scale = 6;
	RoundingMode roundingMode = RoundingMode.HALF_EVEN;
	MonetaryRoundedFactory factory = MonetaryRoundedFactory
			.withRoundingMode(roundingMode).withScale(scale)
			.build();

	assertNotNull(factory);
	MonetaryOperator roundingOperator = factory.getRoundingOperator();
	assertNotNull(roundingOperator);
	assertTrue(ScaleRoundedOperator.class.isInstance(roundingOperator));
	ScaleRoundedOperator result = ScaleRoundedOperator.class.cast(roundingOperator);

	assertEquals(scale, result.getScale());
	assertEquals(roundingMode, result.getRoundingMode());

}
 
Example 2
Source File: PrecisionContextRoundedOperatorTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
	int scale = 4;
	MathContext mathContext = new MathContext(scale, RoundingMode.HALF_EVEN);

	CurrencyUnit real = Monetary.getCurrency("BRL");
	MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real);

	PrecisionContextRoundedOperator monetaryOperator = PrecisionContextRoundedOperator.of(mathContext);
	MonetaryAmount result = monetaryOperator.apply(money);
	assertTrue(RoundedMoney.class.isInstance(result));
	assertEquals(result.getCurrency(), real);
	assertEquals(result.getNumber().getPrecision(), scale);
	assertEquals(BigDecimal.valueOf(35.35), result.getNumber().numberValue(BigDecimal.class));



}
 
Example 3
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * divide(BigDecimal, MathContext)
 */
public void testDivideBigDecimalScaleMathContextHALF_EVEN() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 70;
    int precision = 21;
    RoundingMode rm = RoundingMode.HALF_EVEN;
    MathContext mc = new MathContext(precision, rm);
    String c = "2.77923185514690367475E+26";
    int resScale = -6;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result = aNumber.divide(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", resScale, result.scale());
}
 
Example 4
Source File: TestResource.java    From AVM with MIT License 6 votes vote down vote up
@Callable
public static boolean testShadowJDKEnum(){
    boolean ret = true;
    ret = ret && (RoundingMode.HALF_UP == RoundingMode.valueOf("HALF_UP"));
    ret = ret && (RoundingMode.CEILING instanceof Object);

    RoundingMode[] es = (RoundingMode[]) RoundingMode.values();
    ret = ret && (es[0] == RoundingMode.UP);
    ret = ret && (es[1] == RoundingMode.DOWN);
    ret = ret && (es[2] == RoundingMode.CEILING);
    ret = ret && (es[3] == RoundingMode.FLOOR);
    ret = ret && (es[4] == RoundingMode.HALF_UP);
    ret = ret && (es[5] == RoundingMode.HALF_DOWN);
    ret = ret && (es[6] == RoundingMode.HALF_EVEN);
    ret = ret && (es[7] == RoundingMode.UNNECESSARY);

    return ret;
}
 
Example 5
Source File: ValueDataUtil.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private static MathContext buildMathContext( VariableSpace space ) {
  String precisionString = space.getVariable( Const.KETTLE_BIGDECIMAL_DIVISION_PRECISION );
  String roundingModeString = space.getVariable( Const.KETTLE_BIGDECIMAL_DIVISION_ROUNDING_MODE );

  if ( precisionString != null ) {
    RoundingMode roundingMode;
    try {
      roundingMode = RoundingMode.valueOf( roundingModeString );
    } catch ( IllegalArgumentException | NullPointerException e ) {
      roundingMode = RoundingMode.HALF_EVEN;
    }
    int precision = Integer.parseInt( precisionString );
    if ( precision < 0 ) {
      return MathContext.UNLIMITED;
    } else {
      return new MathContext( precision, roundingMode );
    }
  }
  return MathContext.UNLIMITED;
}
 
Example 6
Source File: DBFRecordBasedResultSet.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * @see org.apache.sis.internal.shapefile.jdbc.resultset.AbstractResultSet#getBigDecimal(java.lang.String)
 * @throws SQLConnectionClosedException if the connection is closed.
 * @throws SQLNoSuchFieldException if the field looked for doesn't exist.
 * @throws SQLNotNumericException if the field value is not numeric.
 */
@Override
public BigDecimal getBigDecimal(String columnLabel) throws SQLConnectionClosedException, SQLNoSuchFieldException, SQLNotNumericException {
    logStep("getBigDecimal", columnLabel);

    assertNotClosed();

    // Act as if we were a double, but store the result in a pre-created BigDecimal at the end.
    try(DBFBuiltInMemoryResultSetForColumnsListing field = (DBFBuiltInMemoryResultSetForColumnsListing)getFieldDesc(columnLabel, sql)) {
        MathContext mc = new MathContext(field.getInt("DECIMAL_DIGITS"), RoundingMode.HALF_EVEN);
        Double doubleValue = getDouble(columnLabel);

        if (doubleValue != null) {
            BigDecimal number = new BigDecimal(doubleValue, mc);
            this.wasNull = false;
            return number;
        }
        else {
            this.wasNull = true;
            return null;
        }
    }
}
 
Example 7
Source File: FloatRoundTo.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a numeric value to round, a number of digits to keep, and an optional rounding mode.
 * @return output returns a single numeric value, the number with only those digits retained
 */
@Override
public Float exec(Tuple input) throws IOException {
    if (input == null || input.size() < 2)
        return null;

    try {
        Float        num    = (Float)input.get(0);
        Integer      digits = (Integer)input.get(1);
        RoundingMode mode   = (input.size() >= 3) ?
            RoundingMode.valueOf(DataType.toInteger(input.get(2))) : RoundingMode.HALF_EVEN;
        if (num == null) return null;

        BigDecimal bdnum  = BigDecimal.valueOf(num);
        bdnum = bdnum.setScale(digits, mode);
        return bdnum.floatValue();
    } catch (Exception e){
        throw new IOException("Caught exception processing input row ", e);
    }
}
 
Example 8
Source File: DBFRecordBasedResultSet.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * @see java.sql.ResultSet#getBigDecimal(java.lang.String, int)
 * @deprecated Deprecated API (from ResultSet Interface)
 * @throws SQLConnectionClosedException if the connection is closed.
 * @throws SQLNoSuchFieldException if the field looked for doesn't exist.
 * @throws SQLNotNumericException if the field value is not numeric.
 */
@Deprecated @Override
public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLConnectionClosedException, SQLNoSuchFieldException, SQLNotNumericException {
    logStep("getBigDecimal", columnLabel, scale);
    assertNotClosed();

    // Act as if we were a double, but store the result in a pre-created BigDecimal at the end.
    MathContext mc = new MathContext(scale, RoundingMode.HALF_EVEN);
    Double doubleValue = getDouble(columnLabel);

    if (doubleValue != null) {
        BigDecimal number = new BigDecimal(getDouble(columnLabel), mc);
        this.wasNull = false;
        return number;
    }
    else {
        this.wasNull = true;
        return null;
    }
}
 
Example 9
Source File: MonetaryRoundedFactoryBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnMathContextOperator() {
	int precision = 6;
	RoundingMode roundingMode = RoundingMode.HALF_EVEN;
	MonetaryRoundedFactory factory = MonetaryRoundedFactory
			.withRoundingMode(roundingMode).withPrecision(precision)
			.build();

	assertNotNull(factory);
	MonetaryOperator roundingOperator = factory.getRoundingOperator();
	assertNotNull(roundingOperator);
	assertTrue(PrecisionContextRoundedOperator.class.isInstance(roundingOperator));
	MathContext result = PrecisionContextRoundedOperator.class.cast(roundingOperator).getMathContext();

	assertEquals(precision, result.getPrecision());
	assertEquals(roundingMode, result.getRoundingMode());

}
 
Example 10
Source File: RoundingMonetaryAmountOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnPositiveValueUsingRoundingTypeAndScale() {
	operator = new RoundingMonetaryAmountOperator(RoundingMode.HALF_EVEN, 3);
	CurrencyUnit currency = Monetary.getCurrency("EUR");
	MonetaryAmount money = Money.parse("EUR 2.3523");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), 2.352);

}
 
Example 11
Source File: DurationFormatter.java    From pikatimer with GNU General Public License v3.0 5 votes vote down vote up
public static final String durationToString(Duration d, String format, String roundingMode){
    Integer precision = 0;
    String[] tokens = format.split("\\.", -1);
    if (tokens.length > 1) precision = tokens[1].length();
    
    Boolean hours = true;
    if (format.contains("[HH:]")) hours = false;
    
    RoundingMode rm = RoundingMode.HALF_EVEN;;
    if (roundingMode.equals("Down")) rm = RoundingMode.DOWN;
    if (roundingMode.equals("Half")) rm = RoundingMode.HALF_EVEN;
    if (roundingMode.equals("Up")) rm = RoundingMode.UP;
    
    return durationToString(d,precision,hours,rm);
}
 
Example 12
Source File: DecimalFormat.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Check validity of using fast-path for this instance. If fast-path is valid
 * for this instance, sets fast-path state as true and initializes fast-path
 * utility fields as needed.
 *
 * This method is supposed to be called rarely, otherwise that will break the
 * fast-path performance. That means avoiding frequent changes of the
 * properties of the instance, since for most properties, each time a change
 * happens, a call to this method is needed at the next format call.
 *
 * FAST-PATH RULES:
 *  Similar to the default DecimalFormat instantiation case.
 *  More precisely:
 *  - HALF_EVEN rounding mode,
 *  - isGroupingUsed() is true,
 *  - groupingSize of 3,
 *  - multiplier is 1,
 *  - Decimal separator not mandatory,
 *  - No use of exponential notation,
 *  - minimumIntegerDigits is exactly 1 and maximumIntegerDigits at least 10
 *  - For number of fractional digits, the exact values found in the default case:
 *     Currency : min = max = 2.
 *     Decimal  : min = 0. max = 3.
 *
 */
private boolean checkAndSetFastPathStatus() {

    boolean fastPathWasOn = isFastPath;

    if ((roundingMode == RoundingMode.HALF_EVEN) &&
        (isGroupingUsed()) &&
        (groupingSize == 3) &&
        (multiplier == 1) &&
        (!decimalSeparatorAlwaysShown) &&
        (!useExponentialNotation)) {

        // The fast-path algorithm is semi-hardcoded against
        //  minimumIntegerDigits and maximumIntegerDigits.
        isFastPath = ((minimumIntegerDigits == 1) &&
                      (maximumIntegerDigits >= 10));

        // The fast-path algorithm is hardcoded against
        //  minimumFractionDigits and maximumFractionDigits.
        if (isFastPath) {
            if (isCurrencyFormat) {
                if ((minimumFractionDigits != 2) ||
                    (maximumFractionDigits != 2))
                    isFastPath = false;
            } else if ((minimumFractionDigits != 0) ||
                       (maximumFractionDigits != 3))
                isFastPath = false;
        }
    } else
        isFastPath = false;

    resetFastPathData(fastPathWasOn);
    fastPathCheckNeeded = false;

    /*
     * Returns true after successfully checking the fast path condition and
     * setting the fast path data. The return value is used by the
     * fastFormat() method to decide whether to call the resetFastPathData
     * method to reinitialize fast path data or is it already initialized
     * in this method.
     */
    return true;
}
 
Example 13
Source File: RoundingModeTests.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 14
Source File: DecimalFormat.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Check validity of using fast-path for this instance. If fast-path is valid
 * for this instance, sets fast-path state as true and initializes fast-path
 * utility fields as needed.
 *
 * This method is supposed to be called rarely, otherwise that will break the
 * fast-path performance. That means avoiding frequent changes of the
 * properties of the instance, since for most properties, each time a change
 * happens, a call to this method is needed at the next format call.
 *
 * FAST-PATH RULES:
 *  Similar to the default DecimalFormat instantiation case.
 *  More precisely:
 *  - HALF_EVEN rounding mode,
 *  - isGroupingUsed() is true,
 *  - groupingSize of 3,
 *  - multiplier is 1,
 *  - Decimal separator not mandatory,
 *  - No use of exponential notation,
 *  - minimumIntegerDigits is exactly 1 and maximumIntegerDigits at least 10
 *  - For number of fractional digits, the exact values found in the default case:
 *     Currency : min = max = 2.
 *     Decimal  : min = 0. max = 3.
 *
 */
private boolean checkAndSetFastPathStatus() {

    boolean fastPathWasOn = isFastPath;

    if ((roundingMode == RoundingMode.HALF_EVEN) &&
        (isGroupingUsed()) &&
        (groupingSize == 3) &&
        (multiplier == 1) &&
        (!decimalSeparatorAlwaysShown) &&
        (!useExponentialNotation)) {

        // The fast-path algorithm is semi-hardcoded against
        //  minimumIntegerDigits and maximumIntegerDigits.
        isFastPath = ((minimumIntegerDigits == 1) &&
                      (maximumIntegerDigits >= 10));

        // The fast-path algorithm is hardcoded against
        //  minimumFractionDigits and maximumFractionDigits.
        if (isFastPath) {
            if (isCurrencyFormat) {
                if ((minimumFractionDigits != 2) ||
                    (maximumFractionDigits != 2))
                    isFastPath = false;
            } else if ((minimumFractionDigits != 0) ||
                       (maximumFractionDigits != 3))
                isFastPath = false;
        }
    } else
        isFastPath = false;

    resetFastPathData(fastPathWasOn);
    fastPathCheckNeeded = false;

    /*
     * Returns true after successfully checking the fast path condition and
     * setting the fast path data. The return value is used by the
     * fastFormat() method to decide whether to call the resetFastPathData
     * method to reinitialize fast path data or is it already initialized
     * in this method.
     */
    return true;
}
 
Example 15
Source File: RoundingModeTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 16
Source File: DecimalFormat.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Check validity of using fast-path for this instance. If fast-path is valid
 * for this instance, sets fast-path state as true and initializes fast-path
 * utility fields as needed.
 *
 * This method is supposed to be called rarely, otherwise that will break the
 * fast-path performance. That means avoiding frequent changes of the
 * properties of the instance, since for most properties, each time a change
 * happens, a call to this method is needed at the next format call.
 *
 * FAST-PATH RULES:
 *  Similar to the default DecimalFormat instantiation case.
 *  More precisely:
 *  - HALF_EVEN rounding mode,
 *  - isGroupingUsed() is true,
 *  - groupingSize of 3,
 *  - multiplier is 1,
 *  - Decimal separator not mandatory,
 *  - No use of exponential notation,
 *  - minimumIntegerDigits is exactly 1 and maximumIntegerDigits at least 10
 *  - For number of fractional digits, the exact values found in the default case:
 *     Currency : min = max = 2.
 *     Decimal  : min = 0. max = 3.
 *
 */
private boolean checkAndSetFastPathStatus() {

    boolean fastPathWasOn = isFastPath;

    if ((roundingMode == RoundingMode.HALF_EVEN) &&
        (isGroupingUsed()) &&
        (groupingSize == 3) &&
        (multiplier == 1) &&
        (!decimalSeparatorAlwaysShown) &&
        (!useExponentialNotation)) {

        // The fast-path algorithm is semi-hardcoded against
        //  minimumIntegerDigits and maximumIntegerDigits.
        isFastPath = ((minimumIntegerDigits == 1) &&
                      (maximumIntegerDigits >= 10));

        // The fast-path algorithm is hardcoded against
        //  minimumFractionDigits and maximumFractionDigits.
        if (isFastPath) {
            if (isCurrencyFormat) {
                if ((minimumFractionDigits != 2) ||
                    (maximumFractionDigits != 2))
                    isFastPath = false;
            } else if ((minimumFractionDigits != 0) ||
                       (maximumFractionDigits != 3))
                isFastPath = false;
        }
    } else
        isFastPath = false;

    resetFastPathData(fastPathWasOn);
    fastPathCheckNeeded = false;

    /*
     * Returns true after successfully checking the fast path condition and
     * setting the fast path data. The return value is used by the
     * fastFormat() method to decide whether to call the resetFastPathData
     * method to reinitialize fast path data or is it already initialized
     * in this method.
     */
    return true;
}
 
Example 17
Source File: DecimalFormat.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Check validity of using fast-path for this instance. If fast-path is valid
 * for this instance, sets fast-path state as true and initializes fast-path
 * utility fields as needed.
 *
 * This method is supposed to be called rarely, otherwise that will break the
 * fast-path performance. That means avoiding frequent changes of the
 * properties of the instance, since for most properties, each time a change
 * happens, a call to this method is needed at the next format call.
 *
 * FAST-PATH RULES:
 *  Similar to the default DecimalFormat instantiation case.
 *  More precisely:
 *  - HALF_EVEN rounding mode,
 *  - isGroupingUsed() is true,
 *  - groupingSize of 3,
 *  - multiplier is 1,
 *  - Decimal separator not mandatory,
 *  - No use of exponential notation,
 *  - minimumIntegerDigits is exactly 1 and maximumIntegerDigits at least 10
 *  - For number of fractional digits, the exact values found in the default case:
 *     Currency : min = max = 2.
 *     Decimal  : min = 0. max = 3.
 *
 */
private boolean checkAndSetFastPathStatus() {

    boolean fastPathWasOn = isFastPath;

    if ((roundingMode == RoundingMode.HALF_EVEN) &&
        (isGroupingUsed()) &&
        (groupingSize == 3) &&
        (multiplier == 1) &&
        (!decimalSeparatorAlwaysShown) &&
        (!useExponentialNotation)) {

        // The fast-path algorithm is semi-hardcoded against
        //  minimumIntegerDigits and maximumIntegerDigits.
        isFastPath = ((minimumIntegerDigits == 1) &&
                      (maximumIntegerDigits >= 10));

        // The fast-path algorithm is hardcoded against
        //  minimumFractionDigits and maximumFractionDigits.
        if (isFastPath) {
            if (isCurrencyFormat) {
                if ((minimumFractionDigits != 2) ||
                    (maximumFractionDigits != 2))
                    isFastPath = false;
            } else if ((minimumFractionDigits != 0) ||
                       (maximumFractionDigits != 3))
                isFastPath = false;
        }
    } else
        isFastPath = false;

    resetFastPathData(fastPathWasOn);
    fastPathCheckNeeded = false;

    /*
     * Returns true after successfully checking the fast path condition and
     * setting the fast path data. The return value is used by the
     * fastFormat() method to decide whether to call the resetFastPathData
     * method to reinitialize fast path data or is it already initialized
     * in this method.
     */
    return true;
}
 
Example 18
Source File: RoundingModeTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 19
Source File: PrecisionScaleRoundedOperatorTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void shouldReturnErrorWhenPrecisionIsZero() {
	MathContext mathContext = new MathContext(0, RoundingMode.HALF_EVEN);
	PrecisionScaleRoundedOperator.of(0, mathContext);
	fail();
}
 
Example 20
Source File: RoundingModeTests.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }