Search in sources :

Example 1 with RoundingMode

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

the class MoneyUtils method initialRound.

/**
     * Does the rounding based on <br />
     * {@link Money#round(Money, BigDecimal, RoundingMode)} with <br />
     * {@link AccountingRules#getInitialRoundOffMultiple()} (Default 1) <br />
     * {@link AccountingRules#getInitialRoundingMode()} (Default
     * {@link RoundingMode#HALF_UP}
     *
     * @param money
     * @return {@link Money}
     */
public static Money initialRound(Money money) {
    BigDecimal initialRoundOffMutiple = AccountingRules.getInitialRoundOffMultiple(money.getCurrency());
    RoundingMode initialRoundingMode = AccountingRules.getInitialRoundingMode();
    return Money.round(money, initialRoundOffMutiple, initialRoundingMode);
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal)

Example 2 with RoundingMode

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

the class MoneyUtils method finalRound.

/**
     * Does the rounding based on <br />
     * {@link Money#round(Money, BigDecimal, RoundingMode)} with <br />
     * {@link AccountingRules#getFinalRoundOffMultiple()} (Default 1) <br />
     * {@link AccountingRules#getFinalRoundingMode()} (Default
     * {@link RoundingMode#CEILING}
     *
     * @param money
     * @return {@link Money}
     */
public static Money finalRound(Money money) {
    BigDecimal finalRoundOffMutiple = AccountingRules.getFinalRoundOffMultiple(money.getCurrency());
    RoundingMode finalRoundingMode = AccountingRules.getFinalRoundingMode();
    return Money.round(money, finalRoundOffMutiple, finalRoundingMode);
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal)

Example 3 with RoundingMode

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

the class AccountingRulesTest method testGetInitialRoundingMode.

@Test
public void testGetInitialRoundingMode() {
    RoundingMode configuredMode = AccountingRules.getInitialRoundingMode();
    String roundingMode = "FLOOR";
    RoundingMode configRoundingMode = RoundingMode.FLOOR;
    MifosConfigurationManager configMgr = MifosConfigurationManager.getInstance();
    configMgr.setProperty(AccountingRulesConstants.INITIAL_ROUNDING_MODE, roundingMode);
    // return value from accounting rules class has to be the value defined
    // in the config file
    assertEquals(configRoundingMode, AccountingRules.getInitialRoundingMode());
    // clear the RoundingRule property from the config file
    configMgr.clearProperty(AccountingRulesConstants.INITIAL_ROUNDING_MODE);
    RoundingMode defaultValue = AccountingRules.getInitialRoundingMode();
    assertEquals(defaultValue, RoundingMode.HALF_UP);
    // now set a wrong rounding mode in config
    roundingMode = "UP";
    configMgr.addProperty(AccountingRulesConstants.INITIAL_ROUNDING_MODE, roundingMode);
    try {
        AccountingRules.getInitialRoundingMode();
        fail();
    } catch (RuntimeException e) {
        assertEquals("InitialRoundingMode defined in the config file is not CEILING, FLOOR, HALF_UP. It is " + roundingMode, e.getMessage());
    }
    // save it back
    configMgr.setProperty(AccountingRulesConstants.INITIAL_ROUNDING_MODE, configuredMode.toString());
}
Also used : RoundingMode(java.math.RoundingMode) MifosConfigurationManager(org.mifos.config.business.MifosConfigurationManager) Test(org.junit.Test)

Example 4 with RoundingMode

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

the class BindingConfigParser method parse.

/**
     * Parses the bindingConfig of an item and returns a WeatherBindingConfig.
     */
public WeatherBindingConfig parse(Item item, String bindingConfig) throws BindingConfigParseException {
    bindingConfig = StringUtils.trimToEmpty(bindingConfig);
    bindingConfig = StringUtils.removeStart(bindingConfig, "{");
    bindingConfig = StringUtils.removeEnd(bindingConfig, "}");
    String[] entries = bindingConfig.split("[,]");
    WeatherBindingConfigHelper helper = new WeatherBindingConfigHelper();
    for (String entry : entries) {
        String[] entryParts = StringUtils.trimToEmpty(entry).split("[=]");
        if (entryParts.length != 2) {
            throw new BindingConfigParseException("A bindingConfig must have a key and a value");
        }
        String key = StringUtils.trim(entryParts[0]);
        String value = StringUtils.trim(entryParts[1]);
        value = StringUtils.removeStart(value, "\"");
        value = StringUtils.removeEnd(value, "\"");
        try {
            helper.getClass().getDeclaredField(key).set(helper, value);
        } catch (Exception e) {
            throw new BindingConfigParseException("Could not set value " + value + " for attribute " + key);
        }
    }
    if (!helper.isValid()) {
        throw new BindingConfigParseException("Invalid binding: " + bindingConfig);
    }
    helper.type = StringUtils.replace(helper.type, "athmosphere", "atmosphere");
    WeatherBindingConfig weatherConfig = null;
    if (helper.isForecast()) {
        Integer forecast = parseInteger(helper.forecast, bindingConfig);
        if (forecast < 0) {
            throw new BindingConfigParseException("Invalid binding, forecast must be >= 0: " + bindingConfig);
        }
        weatherConfig = new ForecastBindingConfig(helper.locationId, forecast, helper.type, helper.property);
    } else {
        weatherConfig = new WeatherBindingConfig(helper.locationId, helper.type, helper.property);
    }
    Weather validationInstance = new Weather(null);
    String property = weatherConfig.getWeatherProperty();
    if (!Weather.isVirtualProperty(property) && !PropertyUtils.hasProperty(validationInstance, property)) {
        throw new BindingConfigParseException("Invalid binding, unknown type or property: " + bindingConfig);
    }
    boolean isDecimalTypeItem = item.getAcceptedDataTypes().contains(DecimalType.class);
    if (isDecimalTypeItem || Weather.isVirtualProperty(property)) {
        RoundingMode roundingMode = RoundingMode.HALF_UP;
        if (helper.roundingMode != null) {
            try {
                roundingMode = RoundingMode.valueOf(StringUtils.upperCase(helper.roundingMode));
            } catch (IllegalArgumentException ex) {
                throw new BindingConfigParseException("Invalid binding, unknown roundingMode: " + bindingConfig);
            }
        }
        Integer scale = 2;
        if (helper.scale != null) {
            scale = parseInteger(helper.scale, bindingConfig);
            if (scale < 0) {
                throw new BindingConfigParseException("Invalid binding, scale must be >= 0: " + bindingConfig);
            }
        }
        weatherConfig.setScale(roundingMode, scale);
    }
    weatherConfig.setUnit(Unit.parse(helper.unit));
    if (StringUtils.isNotBlank(helper.unit) && weatherConfig.getUnit() == null) {
        throw new BindingConfigParseException("Invalid binding, unknown unit: " + bindingConfig);
    }
    try {
        if (!Weather.isVirtualProperty(property) && weatherConfig.hasUnit()) {
            String doubleTypeName = Double.class.getName();
            String propertyTypeName = PropertyUtils.getPropertyTypeName(validationInstance, property);
            if (!StringUtils.equals(doubleTypeName, propertyTypeName)) {
                throw new BindingConfigParseException("Invalid binding, unit specified but property is not a double type: " + bindingConfig);
            }
        }
    } catch (IllegalAccessException ex) {
        logger.error(ex.getMessage(), ex);
        throw new BindingConfigParseException(ex.getMessage());
    }
    return weatherConfig;
}
Also used : RoundingMode(java.math.RoundingMode) ForecastBindingConfig(org.openhab.binding.weather.internal.common.binding.ForecastBindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) Weather(org.openhab.binding.weather.internal.model.Weather) WeatherBindingConfig(org.openhab.binding.weather.internal.common.binding.WeatherBindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 5 with RoundingMode

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

the class TestDecimalRoundingSuite method testEEDecimalScale.

public void testEEDecimalScale() 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");
    }
    doTestEEDecimalScale(roundIsEnabled, roundMode);
}
Also used : RoundingMode(java.math.RoundingMode)

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