Example usage for java.math BigDecimal setScale

List of usage examples for java.math BigDecimal setScale

Introduction

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

Prototype

@Deprecated(since = "9")
public BigDecimal setScale(int newScale, int roundingMode) 

Source Link

Document

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by the appropriate power of ten to maintain its overall value.

Usage

From source file:module.siadap.domain.SiadapEvaluationUniverse.java

private BigDecimal getPonderationResult(BigDecimal scoring, int usedPercentage) {
    BigDecimal percentage = BigDecimal.valueOf(usedPercentage).divide(new BigDecimal(100));

    BigDecimal result = percentage.multiply(scoring);
    return result.setScale(PRECISION, ROUND_MODE);
}

From source file:org.libreplan.business.orders.entities.OrderLine.java

public void setBudget(BigDecimal budget) {
    Validate.isTrue(budget.compareTo(BigDecimal.ZERO) >= 0, "budget cannot be negative");
    this.budget = budget.setScale(2, RoundingMode.HALF_UP);
}

From source file:org.apache.ofbiz.accounting.thirdparty.paypal.PayPalServices.java

public static Map<String, Object> doRefund(DispatchContext dctx, Map<String, Object> context) {
    Locale locale = (Locale) context.get("locale");
    GenericValue payPalConfig = getPaymentMethodGatewayPayPal(dctx, context, null);
    if (payPalConfig == null) {
        return ServiceUtil.returnError(
                UtilProperties.getMessage(resource, "AccountingPayPalPaymentGatewayConfigCannotFind", locale));
    }//from w w w .  j a  v  a  2s .c  om
    GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
    GenericValue captureTrans = PaymentGatewayServices.getCaptureTransaction(orderPaymentPreference);
    BigDecimal refundAmount = (BigDecimal) context.get("refundAmount");
    NVPEncoder encoder = new NVPEncoder();
    encoder.add("METHOD", "RefundTransaction");
    encoder.add("TRANSACTIONID", captureTrans.getString("referenceNum"));
    encoder.add("REFUNDTYPE", "Partial");
    encoder.add("CURRENCYCODE", captureTrans.getString("currencyUomId"));
    encoder.add("AMT", refundAmount.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
    encoder.add("NOTE", "Order #" + orderPaymentPreference.getString("orderId"));
    NVPDecoder decoder = null;
    try {
        decoder = sendNVPRequest(payPalConfig, encoder);
    } catch (PayPalException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }

    if (decoder == null) {
        return ServiceUtil
                .returnError(UtilProperties.getMessage(resource, "AccountingPayPalUnknownError", locale));
    }

    Map<String, Object> result = ServiceUtil.returnSuccess();
    Map<String, String> errors = getErrorMessageMap(decoder);
    if (UtilValidate.isNotEmpty(errors)) {
        result.put("refundResult", false);
        result.put("refundRefNum", captureTrans.getString("referenceNum"));
        result.put("refundAmount", BigDecimal.ZERO);
        if (errors.size() == 1) {
            Map.Entry<String, String> error = errors.entrySet().iterator().next();
            result.put("refundCode", error.getKey());
            result.put("refundMessage", error.getValue());
        } else {
            result.put("refundMessage",
                    "Multiple errors occurred, please refer to the gateway response messages");
            result.put("internalRespMsgs", errors);
        }
    } else {
        result.put("refundResult", true);
        result.put("refundAmount", new BigDecimal(decoder.get("GROSSREFUNDAMT")));
        result.put("refundRefNum", decoder.get("REFUNDTRANSACTIONID"));
    }
    return result;
}

From source file:com.osafe.services.OsafePayPalServices.java

public static Map<String, Object> doRefund(DispatchContext dctx, Map<String, Object> context) {
    Locale locale = (Locale) context.get("locale");
    GenericValue payPalConfig = getPaymentMethodGatewayPayPal(dctx, context, null);
    if (payPalConfig == null) {
        /*            return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
            "AccountingPayPalPaymentGatewayConfigCannotFind", locale));*/
        return ServiceUtil.returnError(
                "Couldn't retrieve a PaymentGatewayConfigPayPal record for Express Checkout, cannot continue.");
    }/*from   w ww .j  av a  2s .c om*/
    GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
    GenericValue captureTrans = PaymentGatewayServices.getCaptureTransaction(orderPaymentPreference);
    BigDecimal refundAmount = (BigDecimal) context.get("refundAmount");
    NVPEncoder encoder = new NVPEncoder();
    encoder.add("METHOD", "RefundTransaction");
    encoder.add("TRANSACTIONID", captureTrans.getString("referenceNum"));
    encoder.add("REFUNDTYPE", "Partial");
    encoder.add("CURRENCYCODE", captureTrans.getString("currencyUomId"));
    encoder.add("AMT", refundAmount.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
    encoder.add("NOTE", "Order #" + orderPaymentPreference.getString("orderId"));
    NVPDecoder decoder = null;
    try {
        decoder = sendNVPRequest(payPalConfig, encoder);
    } catch (PayPalException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }

    if (decoder == null) {
        /*            return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
            "AccountingPayPalUnknownError", locale));*/
        return ServiceUtil.returnError("An unknown error occurred while contacting PayPal");
    }

    Map<String, Object> result = ServiceUtil.returnSuccess();
    Map<String, String> errors = getErrorMessageMap(decoder);
    if (UtilValidate.isNotEmpty(errors)) {
        result.put("refundResult", false);
        result.put("refundRefNum", captureTrans.getString("referenceNum"));
        result.put("refundAmount", BigDecimal.ZERO);
        if (errors.size() == 1) {
            Map.Entry<String, String> error = errors.entrySet().iterator().next();
            result.put("refundCode", error.getKey());
            result.put("refundMessage", error.getValue());
        } else {
            result.put("refundMessage",
                    "Multiple errors occurred, please refer to the gateway response messages");
            result.put("internalRespMsgs", errors);
        }
    } else {
        result.put("refundResult", true);
        result.put("refundAmount", new BigDecimal(decoder.get("GROSSREFUNDAMT")));
        result.put("refundRefNum", decoder.get("REFUNDTRANSACTIONID"));
    }
    return result;
}

From source file:com.salesmanager.core.module.impl.application.currencies.GenericCurrencyModule.java

public String getMeasure(BigDecimal measure, String currencycode) throws Exception {

    NumberFormat nf = null;/*  w  ww  . j  a  v a  2 s .  com*/

    Locale locale = Locale.US;

    if (this.decimalPoint == ',') {
        locale = Locale.GERMAN;
    }

    nf = NumberFormat.getInstance(locale);

    nf.setMaximumFractionDigits(1);
    nf.setMinimumFractionDigits(1);

    measure.setScale(1, BigDecimal.ROUND_HALF_UP);

    return nf.format(measure);
}

From source file:org.businessmanager.web.controller.page.invoice.InvoiceEditController.java

public BigDecimal getTotalVatAmount() {
    BigDecimal totalVatAmount = BigDecimal.ZERO;
    for (LineItemBean lineItem : bean.getLineItems()) {
        BigDecimal amount = lineItem.getVatAmount();
        totalVatAmount = totalVatAmount.add(amount);
    }//  w  ww . ja v a 2s  .c  o m

    return totalVatAmount.setScale(2, RoundingMode.HALF_UP);
}

From source file:org.businessmanager.web.controller.page.invoice.InvoiceEditController.java

public BigDecimal getTotalNetPrice() {
    BigDecimal totalNetPrice = BigDecimal.ZERO;
    for (LineItemBean lineItem : bean.getLineItems()) {
        BigDecimal sumPrice = lineItem.getSumPriceNet();
        totalNetPrice = totalNetPrice.add(sumPrice);
    }//  ww  w .j  av  a  2 s  .c  o m

    return totalNetPrice.setScale(2, RoundingMode.HALF_UP);
}

From source file:org.businessmanager.web.controller.page.invoice.InvoiceEditController.java

public BigDecimal getTotalGrossPrice() {
    BigDecimal totalGrossPrice = BigDecimal.ZERO;
    for (LineItemBean lineItem : bean.getLineItems()) {
        BigDecimal sumPrice = lineItem.getSumPriceGross();
        totalGrossPrice = totalGrossPrice.add(sumPrice);
    }//from ww w .ja  v  a2  s.co m

    return totalGrossPrice.setScale(2, RoundingMode.HALF_UP);
}

From source file:org.cgiar.ccafs.marlo.action.projects.ProjectBudgetByClusterOfActivitiesAction.java

public double round(double value, int places) {
    if (places < 0) {
        throw new IllegalArgumentException();
    }// www . ja  va  2s . c  o  m

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}