Example usage for java.math BigDecimal ROUND_UNNECESSARY

List of usage examples for java.math BigDecimal ROUND_UNNECESSARY

Introduction

In this page you can find the example usage for java.math BigDecimal ROUND_UNNECESSARY.

Prototype

int ROUND_UNNECESSARY

To view the source code for java.math BigDecimal ROUND_UNNECESSARY.

Click Source Link

Document

Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("40");

    BigDecimal bg2 = new BigDecimal("3");

    BigDecimal bg3 = bg1.divide(bg2, BigDecimal.ROUND_UNNECESSARY);

    System.out.println(bg3);//from w ww. j av a  2 s.  c om
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractChannelController.java

private Map<Product, Map<CurrencyValue, Map<String, ProductCharge>>> getProductChargeMap(Channel channel,
        String timeline, Date date, boolean forUpdate) {

    // Structure is of the form::
    ///*from  www  . j a  v a  2  s .  c  om*/
    // "Product":{ "CurrencyVal(forUSD)":{ "catalog": ProductCharge, "rpb": ProductCharge },
    // "CurrencyVal(forINR)": ...

    Map<Product, Map<CurrencyValue, Map<String, ProductCharge>>> fullProductPricingMap = new TreeMap<Product, Map<CurrencyValue, Map<String, ProductCharge>>>(
            new ProductSortOrderSort());

    List<ProductRevision> catalogProductRevisions = new ArrayList<ProductRevision>();
    Map<Product, ProductRevision> rpbProductRevisionMap = new HashMap<Product, ProductRevision>();
    if (timeline.equals("current")) {
        catalogProductRevisions = channelService
                .getChannelRevision(channel, channelService.getCurrentRevision(channel).getStartDate(), false)
                .getProductRevisions();

        rpbProductRevisionMap = channelService
                .getChannelRevision(null, channelService.getCurrentRevision(channel).getStartDate(), false)
                .getProductRevisionsMap();

    } else if (timeline.equals("planned")) {
        catalogProductRevisions = channelService
                .getChannelRevision(channel, channelService.getFutureRevision(channel).getStartDate(), false)
                .getProductRevisions();

        rpbProductRevisionMap = channelService
                .getChannelRevision(null, channelService.getFutureRevision(channel).getStartDate(), false)
                .getProductRevisionsMap();

    } else if (timeline.equals("history")) {
        catalogProductRevisions = channelService.getChannelRevision(channel, date, false).getProductRevisions();

        rpbProductRevisionMap = channelService.getChannelRevision(null, date, false).getProductRevisionsMap();
    }

    for (ProductRevision productRevision : catalogProductRevisions) {
        ProductRevision rpbProductRevision = rpbProductRevisionMap.get(productRevision.getProduct());
        Map<CurrencyValue, Map<String, ProductCharge>> currencyProductPriceMap = new TreeMap<CurrencyValue, Map<String, ProductCharge>>(
                new CurrencyValueSort());
        for (ProductCharge productCharge : productRevision.getProductCharges()) {
            if (currencyProductPriceMap.get(productCharge.getCurrencyValue()) == null) {
                currencyProductPriceMap.put(productCharge.getCurrencyValue(),
                        new LinkedHashMap<String, ProductCharge>());
            }
            if (forUpdate) {
                try {
                    productCharge.setPrice(productCharge.getPrice().setScale(
                            Integer.parseInt(config
                                    .getValue(Names.com_citrix_cpbm_portal_appearance_currency_precision)),
                            BigDecimal.ROUND_UNNECESSARY));
                } catch (ArithmeticException aex) {
                    logger.error("ArithmeticException while editing the product charge, Possible Cause- "
                            + "the currency precision level was reduced " + aex);
                    throw new CurrencyPrecisionException(aex);
                }
            }
            currencyProductPriceMap.get(productCharge.getCurrencyValue()).put("catalog", productCharge);
            if (rpbProductRevision == null) {
                currencyProductPriceMap.get(productCharge.getCurrencyValue()).put("rpb", null);
            }
        }
        if (rpbProductRevision != null) {
            for (ProductCharge productCharge : rpbProductRevision.getProductCharges()) {
                if (currencyProductPriceMap.get(productCharge.getCurrencyValue()) != null) {
                    if (forUpdate) {
                        try {
                            productCharge.setPrice(productCharge.getPrice()
                                    .setScale(Integer.parseInt(config.getValue(
                                            Names.com_citrix_cpbm_portal_appearance_currency_precision)),
                                            BigDecimal.ROUND_UNNECESSARY));
                        } catch (ArithmeticException aex) {
                            logger.error(
                                    "ArithmeticException while editing the product charge, Possible Cause- "
                                            + "the currency precision level was reduced " + aex);
                            throw new CurrencyPrecisionException(aex);
                        }
                    }
                    currencyProductPriceMap.get(productCharge.getCurrencyValue()).put("rpb", productCharge);
                }
            }
        }
        fullProductPricingMap.put(productRevision.getProduct(), currencyProductPriceMap);
    }

    return fullProductPricingMap;
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractChannelController.java

private Map<ProductBundleRevision, Map<CurrencyValue, Map<String, RateCardCharge>>> getBundlePricingMap(
        Channel channel, String timeline, Date date, boolean forUpdate) {
    // Structure is of the form::
    ///*from w  ww  .j  ava 2  s . c o m*/
    // "ProductBundleRevision":{ "CurrencyVal(forUSD)":{ "catalog-onetime": RateCardCharge,
    // "rpb-onetime":RateCardCharge,
    // "catalog-recurring": RateCardCharge,
    // "rpb-recurring": RateCardCharge},
    // "CurrencyVal(forINR)": ...
    // ....

    Map<ProductBundleRevision, Map<CurrencyValue, Map<String, RateCardCharge>>> fullBundlePricingMap = new LinkedHashMap<ProductBundleRevision, Map<CurrencyValue, Map<String, RateCardCharge>>>();

    List<ProductBundleRevision> catalogProductBundleRevisions = new ArrayList<ProductBundleRevision>();
    Map<ProductBundle, ProductBundleRevision> rpbProductBundleRevisionMap = new HashMap<ProductBundle, ProductBundleRevision>();
    if (timeline.equals("current")) {
        catalogProductBundleRevisions = channelService
                .getChannelRevision(channel, channelService.getCurrentRevision(channel).getStartDate(), false)
                .getProductBundleRevisions();

        rpbProductBundleRevisionMap = channelService
                .getChannelRevision(null, channelService.getCurrentRevision(channel).getStartDate(), false)
                .getProductBundleRevisionsMap();

    } else if (timeline.equals("planned")) {
        catalogProductBundleRevisions = channelService
                .getChannelRevision(channel, channelService.getFutureRevision(channel).getStartDate(), false)
                .getProductBundleRevisions();

        rpbProductBundleRevisionMap = channelService
                .getChannelRevision(null, channelService.getFutureRevision(channel).getStartDate(), false)
                .getProductBundleRevisionsMap();
    } else if (timeline.equals("history")) {
        catalogProductBundleRevisions = channelService.getChannelRevision(channel, date, false)
                .getProductBundleRevisions();

        rpbProductBundleRevisionMap = channelService.getChannelRevision(null, date, false)
                .getProductBundleRevisionsMap();
    }

    for (ProductBundleRevision catalaogProductBundleRevision : catalogProductBundleRevisions) {

        Map<CurrencyValue, Map<String, RateCardCharge>> currencyProductBundlePriceMap = new TreeMap<CurrencyValue, Map<String, RateCardCharge>>(
                new CurrencyValueSort());

        ProductBundleRevision rpbProductBundleRevision = rpbProductBundleRevisionMap
                .get(catalaogProductBundleRevision.getProductBundle());

        for (RateCardCharge rcc : catalaogProductBundleRevision.getRateCardCharges()) {
            if (currencyProductBundlePriceMap.get(rcc.getCurrencyValue()) == null) {
                currencyProductBundlePriceMap.put(rcc.getCurrencyValue(),
                        new LinkedHashMap<String, RateCardCharge>());
            }

            if (forUpdate) {
                try {
                    rcc.setPrice(rcc.getPrice().setScale(
                            Integer.parseInt(config
                                    .getValue(Names.com_citrix_cpbm_portal_appearance_currency_precision)),
                            BigDecimal.ROUND_UNNECESSARY));
                } catch (ArithmeticException aex) {
                    logger.error("ArithmeticException while editing the product charge, Possible Cause- "
                            + "the currency precision level was reduced " + aex);
                    throw new CurrencyPrecisionException(aex);

                }
            }
            if (rcc.getRateCardComponent().isRecurring()) {
                currencyProductBundlePriceMap.get(rcc.getCurrencyValue()).put("catalog-recurring", rcc);
            } else {
                currencyProductBundlePriceMap.get(rcc.getCurrencyValue()).put("catalog-onetime", rcc);
            }
        }
        for (RateCardCharge rcc : rpbProductBundleRevision.getRateCardCharges()) {
            if (currencyProductBundlePriceMap.get(rcc.getCurrencyValue()) != null) {
                if (forUpdate) {
                    try {
                        rcc.setPrice(rcc.getPrice().setScale(
                                Integer.parseInt(config
                                        .getValue(Names.com_citrix_cpbm_portal_appearance_currency_precision)),
                                BigDecimal.ROUND_UNNECESSARY));
                    } catch (ArithmeticException aex) {
                        logger.error("ArithmeticException while editing the product charge, Possible Cause- "
                                + "the currency precision level was reduced " + aex);
                        throw new CurrencyPrecisionException(aex);
                    }
                }
                if (rcc.getRateCardComponent().isRecurring()) {
                    currencyProductBundlePriceMap.get(rcc.getCurrencyValue()).put("rpb-recurring", rcc);
                } else {
                    currencyProductBundlePriceMap.get(rcc.getCurrencyValue()).put("rpb-onetime", rcc);
                }
            }
        }
        fullBundlePricingMap.put(catalaogProductBundleRevision, currencyProductBundlePriceMap);
    }
    return fullBundlePricingMap;
}

From source file:Armadillo.Analytics.Base.Precision.java

/**
 * Rounds the given non-negative value to the "nearest" integer. Nearest is
 * determined by the rounding method specified. Rounding methods are defined
 * in {@link BigDecimal}./* ww w.java2  s. co  m*/
 *
 * @param unscaled Value to round.
 * @param sign Sign of the original, scaled value.
 * @param roundingMethod Rounding method, as defined in {@link BigDecimal}.
 * @return the rounded value.
 * @throws MathArithmeticException if an exact operation is required but result is not exact
 * @throws MathIllegalArgumentException if {@code roundingMethod} is not a valid rounding method.
 * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0)
 */
private static double roundUnscaled(double unscaled, double sign, int roundingMethod)
        throws MathArithmeticException, MathIllegalArgumentException {
    switch (roundingMethod) {
    case BigDecimal.ROUND_CEILING:
        if (sign == -1) {
            unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
        } else {
            unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY));
        }
        break;
    case BigDecimal.ROUND_DOWN:
        unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
        break;
    case BigDecimal.ROUND_FLOOR:
        if (sign == -1) {
            unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY));
        } else {
            unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
        }
        break;
    case BigDecimal.ROUND_HALF_DOWN: {
        unscaled = FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY);
        double fraction = unscaled - FastMath.floor(unscaled);
        if (fraction > 0.5) {
            unscaled = FastMath.ceil(unscaled);
        } else {
            unscaled = FastMath.floor(unscaled);
        }
        break;
    }
    case BigDecimal.ROUND_HALF_EVEN: {
        double fraction = unscaled - FastMath.floor(unscaled);
        if (fraction > 0.5) {
            unscaled = FastMath.ceil(unscaled);
        } else if (fraction < 0.5) {
            unscaled = FastMath.floor(unscaled);
        } else {
            // The following equality test is intentional and needed for rounding purposes
            if (FastMath.floor(unscaled) / 2.0 == FastMath.floor(Math.floor(unscaled) / 2.0)) { // even
                unscaled = FastMath.floor(unscaled);
            } else { // odd
                unscaled = FastMath.ceil(unscaled);
            }
        }
        break;
    }
    case BigDecimal.ROUND_HALF_UP: {
        unscaled = FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY);
        double fraction = unscaled - FastMath.floor(unscaled);
        if (fraction >= 0.5) {
            unscaled = FastMath.ceil(unscaled);
        } else {
            unscaled = FastMath.floor(unscaled);
        }
        break;
    }
    case BigDecimal.ROUND_UNNECESSARY:
        if (unscaled != FastMath.floor(unscaled)) {
            throw new MathArithmeticException();
        }
        break;
    case BigDecimal.ROUND_UP:
        unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY));
        break;
    default:
        throw new MathIllegalArgumentException(LocalizedFormats.INVALID_ROUNDING_METHOD, roundingMethod,
                "ROUND_CEILING", BigDecimal.ROUND_CEILING, "ROUND_DOWN", BigDecimal.ROUND_DOWN, "ROUND_FLOOR",
                BigDecimal.ROUND_FLOOR, "ROUND_HALF_DOWN", BigDecimal.ROUND_HALF_DOWN, "ROUND_HALF_EVEN",
                BigDecimal.ROUND_HALF_EVEN, "ROUND_HALF_UP", BigDecimal.ROUND_HALF_UP, "ROUND_UNNECESSARY",
                BigDecimal.ROUND_UNNECESSARY, "ROUND_UP", BigDecimal.ROUND_UP);
    }
    return unscaled;
}