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:com.ar.dev.tierra.api.dao.impl.FiscalDAOImpl.java

@Override
public void factura_a(List<DetalleFactura> detalles, Cliente cliente) {
    try (PrintWriter ticket = new PrintWriter("command/factura_a.200")) {
        DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMaximumFractionDigits(1);
        ticket.println("b" + (char) 28 + cliente.getNombreCliente() + (char) 28 + cliente.getDocumento()
                + (char) 28 + cliente.getResponsabilidadIva() + (char) 28 + cliente.getTipoDocumento()
                + (char) 28 + cliente.getDomicilio());
        ticket.println("@" + (char) 28 + "A" + (char) 28 + "T");
        BigDecimal descuento = new BigDecimal(BigInteger.ZERO);
        for (DetalleFactura detalle : detalles) {
            if (detalle.getDescuentoDetalle() != null) {
                descuento = descuento.add(detalle.getDescuentoDetalle());
            }/*from   w  w w  . ja v a 2  s  .c  o  m*/
            String price = null;
            BigDecimal sinIVA = detalle.getProducto().getPrecioVenta().subtract(detalle.getProducto()
                    .getPrecioVenta().multiply(new BigDecimal(17.35)).divide(new BigDecimal(100)));
            price = sinIVA.setScale(4, RoundingMode.HALF_UP).toString();
            ticket.println("B" + (char) 28 /*Abrimos linea*/
                    + detalle.getProducto().getDescripcion() + (char) 28 /*Nombre producto*/
                    + detalle.getCantidadDetalle() + ".0" + (char) 28 /*Cantidad*/
                    + price.replace(",", ".") + (char) 28 /*Precio unitario*/
                    + "21.0" + (char) 28 /*Impuestos IVA*/
                    + "M" + (char) 28 /*Suma monto*/
                    + "0.0" + (char) 28 /*Impuestos internos*/
                    + "0" + (char) 28 /*Parametro display*/
                    + "b");
            /*Cierra de linea*/
        }
        if (!descuento.equals(new BigDecimal(BigInteger.ZERO))) {
            ticket.println("T" + (char) 28 /*Abrimos linea descuento*/
                    + "Descuento: " + (char) 28 /*Texto a mostrar*/
                    + descuento + (char) 28 /*Monto descuento*/
                    + "m" + (char) 28 /*m: descuento, M: aumento*/
                    + "0" + (char) 28 /*parametro display*/
                    + "T");
            /*cierre linea descuento*/
        }
        ticket.println("E");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FiscalDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.ar.dev.tierra.api.dao.impl.FiscalDAOImpl.java

@Override
public void factura_b(List<DetalleFactura> detalles, Cliente cliente) {
    try (PrintWriter ticket = new PrintWriter("command/factura_b.200")) {
        DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMaximumFractionDigits(1);
        ticket.println("b" + (char) 28 + cliente.getNombreCliente() + (char) 28 + cliente.getDocumento()
                + (char) 28 + cliente.getResponsabilidadIva() + (char) 28 + cliente.getTipoDocumento()
                + (char) 28 + cliente.getDomicilio());
        ticket.println("@" + (char) 28 + "B" + (char) 28 + "T");
        BigDecimal descuento = new BigDecimal(BigInteger.ZERO);
        for (DetalleFactura detalle : detalles) {
            if (detalle.getDescuentoDetalle() != null) {
                descuento = descuento.add(detalle.getDescuentoDetalle());
            }/*  w ww . ja  va  2  s . c  o  m*/
            String price = null;
            BigDecimal sinIVA = detalle.getProducto().getPrecioVenta().subtract(detalle.getProducto()
                    .getPrecioVenta().multiply(new BigDecimal(17.35)).divide(new BigDecimal(100)));
            price = sinIVA.setScale(4, RoundingMode.HALF_UP).toString();
            ticket.println("B" + (char) 28 /*Abrimos linea*/
                    + detalle.getProducto().getDescripcion() + (char) 28 /*Nombre producto*/
                    + detalle.getCantidadDetalle() + ".0" + (char) 28 /*Cantidad*/
                    + price.replace(",", ".") + (char) 28 /*Precio unitario*/
                    + "21.0" + (char) 28 /*Impuestos IVA*/
                    + "M" + (char) 28 /*Suma monto*/
                    + "0.0" + (char) 28 /*Impuestos internos*/
                    + "0" + (char) 28 /*Parametro display*/
                    + "b");
            /*Cierra de linea*/
        }
        if (!descuento.equals(new BigDecimal(BigInteger.ZERO))) {
            ticket.println("T" + (char) 28 /*Abrimos linea descuento*/
                    + "Descuento: " + (char) 28 /*Texto a mostrar*/
                    + descuento + (char) 28 /*Monto descuento*/
                    + "m" + (char) 28 /*m: descuento, M: aumento*/
                    + "0" + (char) 28 /*parametro display*/
                    + "T");
            /*cierre linea descuento*/
        }
        ticket.println("E");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FiscalDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.kalypso.model.wspm.tuhh.core.wprof.BCEShapeWPRofContentProvider.java

@Override
public BigDecimal getDistance() {
    checkPropertyExists("DISTANCE");//$NON-NLS-1$

    final double distance = getProperty("DISTANCE", Double.class, Double.NaN); //$NON-NLS-1$
    if (Double.isNaN(distance))
        return null;

    final BigDecimal bigDecimal = new BigDecimal(distance);
    bigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP);
    return bigDecimal;
}

From source file:dk.clanie.money.Money.java

public Money(BigDecimal amount, Currency currency, RoundingMode roundingMode) {
    int scale = currency.getDefaultFractionDigits();
    if (scale < 0)
        scale = 6;//w  ww.  jav a2s .  com
    this.amount = amount.setScale(scale, roundingMode);
    this.currency = currency;
}

From source file:gg.view.overview.IncomeExpensesTopComponent.java

/** Displays the total income vs expenses for the current month */
public void displayData() {
    log.info("Income vs Expenses graph computed and displayed");

    // Display hourglass cursor
    Utilities.changeCursorWaitStatus(true);

    // Create a dataset (the dataset will contain the plotted values)
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    // Create an empty chart
    JFreeChart chart = ChartFactory.createBarChart("", // chart title
            "", // x axis label
            NbBundle.getMessage(IncomeExpensesTopComponent.class, "IncomeExpensesTopComponent.Amount"), // y axis label
            dataset, // data displayed in the chart
            PlotOrientation.VERTICAL, false, // include legend
            true, // tooltips
            false // urls
    );/*  ww  w .j a  v a  2s  . c om*/

    // Update the chart color
    chart.setBackgroundPaint(jPanelIncomeExpenses.getBackground());
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.WHITE);

    // Set the orientation of the categories on the domain axis (X axis)
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);

    // Set the range axis (Y axis) to display integers only
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // Set the bar renderer
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);
    renderer.setMaximumBarWidth(0.1);

    GradientPaint gradientPaint = new GradientPaint(0.0f, 0.0f, new Color(49, 106, 196), 0.0f, 0.0f,
            Color.LIGHT_GRAY);
    renderer.setSeriesPaint(0, gradientPaint);

    // Create a period for the current month
    LocalDate today = new LocalDate();
    Period currentMonth = new Period(Periods.getAdjustedStartDate(today, PeriodType.MONTH),
            Periods.getAdjustedEndDate(today, PeriodType.MONTH), PeriodType.MONTH);

    // Fill the dataset
    List<Currency> currencies = Wallet.getInstance().getActiveCurrencies();
    for (Currency currency : currencies) {
        // Filter on the currency and on the current month
        SearchCriteria searchCriteria = new SearchCriteria(currency, null, currentMonth, null, null, null,
                false);

        // Get income
        BigDecimal currencyIncome = Datamodel.getIncome(searchCriteria);
        currencyIncome = currencyIncome.setScale(2, RoundingMode.HALF_EVEN);

        // Get expenses
        BigDecimal currencyExpenses = Datamodel.getExpenses(searchCriteria).abs();
        currencyExpenses = currencyExpenses.setScale(2, RoundingMode.HALF_EVEN);

        // Plot income and expenses for the current month and for the current currency on the chart
        dataset.addValue(currencyIncome, currency.getName(),
                NbBundle.getMessage(IncomeExpensesTopComponent.class, "IncomeExpensesTopComponent.Income",
                        new Object[] { currency }));

        dataset.addValue(currencyExpenses, currency.getName(),
                NbBundle.getMessage(IncomeExpensesTopComponent.class, "IncomeExpensesTopComponent.Expenses",
                        new Object[] { currency }));
    }

    // Create the chart panel that contains the chart
    ChartPanel chartPanel = new ChartPanel(chart);

    // Display the chart
    jPanelIncomeExpenses.removeAll();
    jPanelIncomeExpenses.add(chartPanel, BorderLayout.CENTER);

    // Display the normal cursor
    Utilities.changeCursorWaitStatus(false);
}

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

private static void addCartDetails(NVPEncoder encoder, ShoppingCart cart) throws GenericEntityException {
    encoder.add("CURRENCYCODE", cart.getCurrency());
    int line = 0;
    for (ShoppingCartItem item : cart.items()) {
        encoder.add("L_NUMBER" + line, item.getProductId());
        encoder.add("L_NAME" + line, item.getName());
        encoder.add("L_AMT" + line, item.getBasePrice().setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        encoder.add("L_QTY" + line, item.getQuantity().toBigInteger().toString());
        line++;//  w ww .  j  av a  2  s  .co  m
        BigDecimal otherAdjustments = item.getOtherAdjustments();
        if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) {
            encoder.add("L_NUMBER" + line, item.getProductId());
            encoder.add("L_NAME" + line, item.getName() + " Adjustments");
            encoder.add("L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
            encoder.add("L_QTY" + line, "1");
            line++;
        }
    }
    BigDecimal otherAdjustments = cart.getOrderOtherAdjustmentTotal();
    if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) {
        encoder.add("L_NUMBER" + line, "N/A");
        encoder.add("L_NAME" + line, "Order Adjustments");
        encoder.add("L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        encoder.add("L_QTY" + line, "1");
        line++;
    }
    encoder.add("ITEMAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
    encoder.add("SHIPPINGAMT", "0.00");
    encoder.add("TAXAMT", "0.00");
    encoder.add("AMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
    //NOTE: The docs say this is optional but then won't work without it
    encoder.add("MAXAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
}

From source file:com.example.photoremark.MainActivity.java

public double roundDouble(double value, int weishu) {
    BigDecimal bg = new BigDecimal(value);
    double newValue = bg.setScale(weishu, BigDecimal.ROUND_HALF_UP).doubleValue();
    return newValue;
}

From source file:com.benfante.minimark.po.Assessment.java

@Transient
public BigDecimal getQuestionsTotalWeight() {
    double result = 0;
    for (AssessmentQuestion assessmentQuestion : getQuestions()) {
        final Question question = assessmentQuestion.getQuestion();
        if (question.getWeight() != null) {
            result += question.getWeight().doubleValue();
        }//from  ww  w . j  a va  2 s.  c o  m
    }
    final BigDecimal bdResult = BigDecimal.valueOf(result);
    bdResult.setScale(2, RoundingMode.HALF_EVEN);
    return bdResult;
}

From source file:op.tools.SYSTools.java

public static String roundScale2(BigDecimal bd) {
    return bd.setScale(2, BigDecimal.ROUND_HALF_EVEN).toPlainString();
}

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

private static void addCartDetails(NVPEncoder encoder, ShoppingCart cart) throws GenericEntityException {
    encoder.add("CURRENCYCODE", cart.getCurrency());
    int line = 0;
    for (ShoppingCartItem item : cart.items()) {
        encoder.add("L_NUMBER" + line, item.getProductId());
        encoder.add("L_NAME" + line, item.getName());
        encoder.add("L_AMT" + line, item.getBasePrice().setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        encoder.add("L_QTY" + line, item.getQuantity().toBigInteger().toString());
        line++;//  w  w w .ja va2s  . co  m
        BigDecimal otherAdjustments = item.getOtherAdjustments();
        if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) {
            encoder.add("L_NUMBER" + line, item.getProductId());
            encoder.add("L_NAME" + line, item.getName() + " Adjustments");
            encoder.add("L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
            encoder.add("L_QTY" + line, "1");
            line++;
        }
    }
    BigDecimal otherAdjustments = cart.getOrderOtherAdjustmentTotal();
    if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) {
        encoder.add("L_NUMBER" + line, "N/A");
        encoder.add("L_NAME" + line, "Order Adjustments");
        encoder.add("L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        encoder.add("L_QTY" + line, "1");
        line++;
    }
    encoder.add("ITEMAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
    //encoder.add("SHIPPINGAMT", "0.00");
    //encoder.add("TAXAMT", "0.00");
    encoder.add("SHIPPINGAMT", cart.getTotalShipping().setScale(2).toPlainString());
    encoder.add("TAXAMT", cart.getTotalSalesTax().setScale(2).toPlainString());
    encoder.add("AMT", cart.getGrandTotal().setScale(2).toPlainString());
    //encoder.add("AMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
    //NOTE: The docs say this is optional but then won't work without it
    //encoder.add("MAXAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
}