Search in sources :

Example 71 with RoundingMode

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

the class decimal method multiply.

public static final void multiply(IData pipeline) throws ServiceException {
    // --- <<IS-START(multiply)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] record:0:optional $operands
    // [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 {
        IData operands = IDataHelper.get(cursor, "$operands", IData.class);
        int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
        RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
        // support $decimals and $decimal input for backwards-compatibility
        boolean backwardsCompatiblityRequired = false;
        if (operands == null) {
            String[] list = IDataHelper.get(cursor, "$decimals", String[].class);
            String decimal = IDataHelper.get(cursor, "$decimal", String.class);
            IDataMap map = new IDataMap();
            if (list != null)
                map.put("$decimals", list);
            if (decimal != null)
                map.put("$decimal", decimal);
            operands = map;
            backwardsCompatiblityRequired = true;
        }
        BigDecimal[] decimals = BigDecimalHelper.normalize(IDataHelper.getLeafValues(operands));
        BigDecimal result = BigDecimalHelper.round(BigDecimalHelper.multiply(decimals), precision, rounding);
        if (backwardsCompatiblityRequired) {
            IDataHelper.put(cursor, "$decimal", result, String.class, false);
        } else {
            IDataHelper.put(cursor, "$result", result, String.class, false);
        }
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : RoundingMode(java.math.RoundingMode) IDataMap(permafrost.tundra.data.IDataMap) BigDecimal(java.math.BigDecimal)

Example 72 with RoundingMode

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

the class decimal method subtract.

public static final void subtract(IData pipeline) throws ServiceException {
    // --- <<IS-START(subtract)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] field:0:optional $minuend
    // [i] field:0:optional $subtrahend
    // [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 minuend = IDataHelper.get(cursor, "$minuend", BigDecimal.class);
        BigDecimal subtrahend = IDataHelper.get(cursor, "$subtrahend", BigDecimal.class);
        int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
        RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
        // support $decimal.x and $decimal.y inputs for backwards-compatibility
        boolean backwardsCompatiblityRequired = false;
        if (minuend == null && subtrahend == null) {
            minuend = IDataHelper.get(cursor, "$decimal.x", BigDecimal.class);
            subtrahend = IDataHelper.get(cursor, "$decimal.y", BigDecimal.class);
            backwardsCompatiblityRequired = true;
        }
        BigDecimal result = BigDecimalHelper.round(BigDecimalHelper.subtract(minuend, subtrahend), precision, rounding);
        if (backwardsCompatiblityRequired) {
            IDataHelper.put(cursor, "$decimal", result, String.class, false);
        } else {
            IDataHelper.put(cursor, "$result", result, String.class, false);
        }
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal)

Example 73 with RoundingMode

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

the class decimal method minimum.

public static final void minimum(IData pipeline) throws ServiceException {
    // --- <<IS-START(minimum)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] record:0:optional $operands
    // [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 $minimum
    IDataCursor cursor = pipeline.getCursor();
    try {
        IData operands = IDataHelper.get(cursor, "$operands", IData.class);
        int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
        RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
        // support $decimals input for backwards-compatibility
        boolean backwardsCompatiblityRequired = false;
        if (operands == null) {
            String[] list = IDataHelper.get(cursor, "$decimals", String[].class);
            IDataMap map = new IDataMap();
            if (list != null)
                map.put("$decimals", list);
            operands = map;
            backwardsCompatiblityRequired = true;
        }
        BigDecimal[] decimals = BigDecimalHelper.normalize(IDataHelper.getLeafValues(operands));
        BigDecimal result = BigDecimalHelper.round(BigDecimalHelper.minimum(decimals), precision, rounding);
        if (backwardsCompatiblityRequired) {
            IDataHelper.put(cursor, "$decimal", result, String.class, false);
        } else {
            IDataHelper.put(cursor, "$minimum", result, String.class, false);
        }
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : RoundingMode(java.math.RoundingMode) IDataMap(permafrost.tundra.data.IDataMap) BigDecimal(java.math.BigDecimal)

Example 74 with RoundingMode

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

the class decimal method maximum.

public static final void maximum(IData pipeline) throws ServiceException {
    // --- <<IS-START(maximum)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] record:0:optional $operands
    // [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 $maximum
    IDataCursor cursor = pipeline.getCursor();
    try {
        IData operands = IDataHelper.get(cursor, "$operands", IData.class);
        int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
        RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
        // support $decimals input for backwards-compatibility
        boolean backwardsCompatiblityRequired = false;
        if (operands == null) {
            String[] list = IDataHelper.get(cursor, "$decimals", String[].class);
            IDataMap map = new IDataMap();
            if (list != null)
                map.put("$decimals", list);
            operands = map;
            backwardsCompatiblityRequired = true;
        }
        BigDecimal[] decimals = BigDecimalHelper.normalize(IDataHelper.getLeafValues(operands));
        BigDecimal result = BigDecimalHelper.round(BigDecimalHelper.maximum(decimals), precision, rounding);
        if (backwardsCompatiblityRequired) {
            IDataHelper.put(cursor, "$decimal", result, String.class, false);
        } else {
            IDataHelper.put(cursor, "$maximum", result, String.class, false);
        }
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : RoundingMode(java.math.RoundingMode) IDataMap(permafrost.tundra.data.IDataMap) BigDecimal(java.math.BigDecimal)

Example 75 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)

Aggregations

RoundingMode (java.math.RoundingMode)206 BigDecimal (java.math.BigDecimal)127 BigInteger (java.math.BigInteger)88 MathContext (java.math.MathContext)72 GwtIncompatible (com.google.common.annotations.GwtIncompatible)39 DecimalFormat (java.text.DecimalFormat)5 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 ButtonGroup (javax.swing.ButtonGroup)2 JLabel (javax.swing.JLabel)2 JPanel (javax.swing.JPanel)2 JRadioButton (javax.swing.JRadioButton)2 JSpinner (javax.swing.JSpinner)2 SpinnerNumberModel (javax.swing.SpinnerNumberModel)2