Search in sources :

Example 6 with RoundingMode

use of java.math.RoundingMode in project voltdb by VoltDB.

the class TestDecimalRoundingSuite method testDecimalScaleInsertion.

public void testDecimalScaleInsertion() throws Exception {
    Boolean roundIsEnabled = Boolean.valueOf(m_defaultRoundingEnablement);
    RoundingMode roundMode = RoundingMode.valueOf(m_defaultRoundingMode);
    assert (m_config instanceof LocalCluster);
    LocalCluster localCluster = (LocalCluster) m_config;
    Map<String, String> props = localCluster.getAdditionalProcessEnv();
    if (props != null) {
        roundIsEnabled = Boolean.valueOf(props.containsKey(m_roundingEnabledProperty) ? props.get(m_roundingEnabledProperty) : "true");
        roundMode = RoundingMode.valueOf(props.containsKey(m_roundingModeProperty) ? props.get(m_roundingModeProperty) : "HALF_UP");
        System.out.printf("Rounding is %senabled, mode is %s\n", roundIsEnabled ? "" : "not ", roundMode.toString());
    } else {
        System.out.printf("Default rounding (%s), Default Rounding mode (%s).\n", roundIsEnabled.toString(), roundMode.toString());
    }
    doTestDecimalScaleInsertion(roundIsEnabled, roundMode);
}
Also used : RoundingMode(java.math.RoundingMode)

Example 7 with RoundingMode

use of java.math.RoundingMode in project Tundra by Permafrost.

the class measure method convert.

// ---( server methods )---
public static final void convert(IData pipeline) throws ServiceException {
    // --- <<IS-START(convert)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] field:0:optional $value
    // [i] field:0:required $unit.input {"t","kg","g","lb","oz","km","m","cm","mm","mi","yd","ft","in"}
    // [i] field:0:required $unit.output {"t","kg","g","lb","oz","km","m","cm","mm","mi","yd","ft","in"}
    // [i] field:0:optional $precision
    // [i] field:0:optional $rounding {"HALF_UP","CEILING","DOWN","FLOOR","HALF_DOWN","HALF_EVEN","UNNECESSARY","UP"}
    // [o] field:0:optional $result
    IDataCursor cursor = pipeline.getCursor();
    try {
        BigDecimal value = IDataHelper.get(cursor, "$value", BigDecimal.class);
        String inputUnit = IDataHelper.get(cursor, "$unit.input", String.class);
        String outputUnit = IDataHelper.get(cursor, "$unit.output", String.class);
        int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
        RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
        if (value != null) {
            IDataHelper.put(cursor, "$result", BigDecimalHelper.round(MeasureHelper.convert(value, UnitHelper.parse(inputUnit), UnitHelper.parse(outputUnit), MathContext.DECIMAL128), precision, rounding), String.class);
        }
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal)

Example 8 with RoundingMode

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

the class OldBigDecimalConstructorsTest 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());
    // Now test more than just RoundingMode.CEILING
    //
    BigDecimal bd;
    mc = new MathContext(15, RoundingMode.UP);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.89012345678902E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.89012345678902E+16", bd.toString());
    mc = new MathContext(12, RoundingMode.DOWN);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.89012345678E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.89012345678E+16", bd.toString());
    mc = new MathContext(15, RoundingMode.CEILING);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.89012345678902E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.89012345678901E+16", bd.toString());
    mc = new MathContext(12, RoundingMode.FLOOR);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.89012345678E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.89012345679E+16", bd.toString());
    mc = new MathContext(16, RoundingMode.HALF_EVEN);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.890123456789012E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.890123456789012E+16", bd.toString());
    bd = new BigDecimal(-78901234567890135L, mc);
    assertEquals("incorrect value", "-7.890123456789014E+16", bd.toString());
    mc = new MathContext(16, RoundingMode.HALF_UP);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.890123456789013E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.890123456789013E+16", bd.toString());
    mc = new MathContext(16, RoundingMode.HALF_DOWN);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.890123456789012E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.890123456789012E+16", bd.toString());
    mc = new MathContext(8, RoundingMode.UNNECESSARY);
    try {
        bd = new BigDecimal(78901234567890125L, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
    try {
        bd = new BigDecimal(-78901234567890125L, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 9 with RoundingMode

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

the class OldBigDecimalConstructorsTest method testConstrCharIntIntMathContextException1.

/**
     * new BigDecimal(char[] value, int offset, int len, MathContext mc);
     */
public void testConstrCharIntIntMathContextException1() {
    char[] value = { '-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3' };
    int offset = 3;
    int len = 120;
    int precision = 4;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    try {
        new BigDecimal(value, offset, len, mc);
        fail("NumberFormatException has not been thrown");
    } catch (NumberFormatException e) {
    }
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 10 with RoundingMode

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

the class OldBigDecimalConstructorsTest method testConstrStringMathContext.

/**
     * new BigDecimal(String value, MathContext)
     */
public void testConstrStringMathContext() {
    String a = "-238768787678287e214";
    int precision = 5;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    String res = "-23876";
    int resScale = -224;
    BigDecimal result = new BigDecimal(a, mc);
    assertEquals("incorrect value", res, result.unscaledValue().toString());
    assertEquals("incorrect scale", resScale, result.scale());
    // Now test more than just RoundingMode.CEILING:
    //
    String biStr = new String("12345678901234567890123456789012345.0E+10");
    String nbiStr = new String("-12345678901234567890123456789012345.E+10");
    BigDecimal bd;
    mc = new MathContext(31, RoundingMode.UP);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.234567890123456789012345678902E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString());
    mc = new MathContext(28, RoundingMode.DOWN);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.234567890123456789012345678E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString());
    mc = new MathContext(33, RoundingMode.CEILING);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.23456789012345678901234567890124E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString());
    mc = new MathContext(34, RoundingMode.UNNECESSARY);
    try {
        bd = new BigDecimal(biStr, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
    try {
        bd = new BigDecimal(nbiStr, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
    mc = new MathContext(7, RoundingMode.FLOOR);
    bd = new BigDecimal("1000000.9", mc);
    assertEquals("incorrect value", "1000000", bd.toString());
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Aggregations

RoundingMode (java.math.RoundingMode)254 BigDecimal (java.math.BigDecimal)139 BigInteger (java.math.BigInteger)88 MathContext (java.math.MathContext)86 GwtIncompatible (com.google.common.annotations.GwtIncompatible)39 Test (org.testng.annotations.Test)13 NumberFormat (java.text.NumberFormat)10 MonetaryOperator (javax.money.MonetaryOperator)8 DecimalFormat (java.text.DecimalFormat)6 IDataMap (permafrost.tundra.data.IDataMap)5 Test (org.junit.Test)3 MifosConfigurationManager (org.mifos.config.business.MifosConfigurationManager)3 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Insets (java.awt.Insets)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 MonetaryContext (javax.money.MonetaryContext)2 ButtonGroup (javax.swing.ButtonGroup)2 JLabel (javax.swing.JLabel)2