Example usage for java.math RoundingMode HALF_UP

List of usage examples for java.math RoundingMode HALF_UP

Introduction

In this page you can find the example usage for java.math RoundingMode HALF_UP.

Prototype

RoundingMode HALF_UP

To view the source code for java.math RoundingMode HALF_UP.

Click Source Link

Document

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

Usage

From source file:com.floreantpos.util.NumberUtil.java

public static double roundToThreeDigit(double value) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(3, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:org.techytax.helper.AmountHelper.java

public static BigDecimal round(BigDecimal amount) {
    if (amount != null) {
        return amount.setScale(2, RoundingMode.HALF_UP);
    } else {/*from w ww . ja v  a  2  s .  c om*/
        return null;
    }
}

From source file:org.cirdles.ludwig.squid25.Utilities.java

/**
 * Performs excel-style rounding of double to a given number of significant
 * figures./*from www .  j  a  v a2 s.c  o  m*/
 *
 * @param value double to round
 * @param sigFigs count of significant digits for rounding
 * @return double rounded to sigFigs significant digits
 */
public static double roundedToSize(double value, int sigFigs) {
    BigDecimal valueBDtoSize = BigDecimal.ZERO;
    if (Double.isFinite(value)) {
        BigDecimal valueBD = new BigDecimal(value);
        int newScale = sigFigs - (valueBD.precision() - valueBD.scale());
        valueBDtoSize = valueBD.setScale(newScale, RoundingMode.HALF_UP);
    }
    return valueBDtoSize.doubleValue();
}

From source file:Main.java

public Main() {
    formTextFieldFormat = NumberFormat.getNumberInstance();
    formTextFieldFormat.setMinimumFractionDigits(2);
    formTextFieldFormat.setMaximumFractionDigits(2);
    formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);

    focusLabel.setPreferredSize(new Dimension(120, 27));
    formTextField = new JFormattedTextField(formTextFieldFormat);
    formTextField.setValue(amount);/*w  w w  .  ja  va 2 s. c  o  m*/
    formTextField.setPreferredSize(new Dimension(120, 27));
    formTextField.setHorizontalAlignment(SwingConstants.RIGHT);
    formTextField.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            formTextField.requestFocus();
            formTextField.setText(formTextField.getText());
            formTextField.selectAll();
        }

        @Override
        public void focusLost(FocusEvent e) {
            double t1a1 = (((Number) formTextField.getValue()).doubleValue());
            if (t1a1 < 1000) {
                formTextField.setValue(amount);
            }
        }
    });

    docLabel.setPreferredSize(new Dimension(120, 27));

    formTextField1 = new JFormattedTextField(formTextFieldFormat);
    formTextField1.setValue(amount);

    formTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
    formTextField1.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            formTextField1.requestFocus();
            formTextField1.setText(formTextField1.getText());
            formTextField1.selectAll();
        }

        @Override
        public void focusLost(FocusEvent e) {
        }
    });
    formTextField1.getDocument().addDocumentListener(docListener);

    pnl = new JPanel();
    pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
    pnl.setLayout(new GridLayout(2, 2));
    pnl.add(focusLabel);
    pnl.add(formTextField);
    pnl.add(docLabel);
    pnl.add(formTextField1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pnl, BorderLayout.CENTER);
    frame.setLocation(200, 200);
    frame.pack();
    frame.setVisible(true);
    formTextFieldFocus1();
}

From source file:org.techytax.helper.AmountHelper.java

public static BigInteger roundToInteger(BigDecimal amount) {
    if (amount != null) {
        return amount.setScale(0, RoundingMode.HALF_UP).toBigInteger();
    } else {/*w ww. j  a v  a2 s  .  c  o m*/
        return null;
    }
}

From source file:org.openvpms.archetype.rules.math.MathRules.java

/**
 * Rounds a value./*  w  w w .  ja v a2s . c o m*/
 *
 * @param value the value
 * @param scale the no. of decimal places
 * @return the rounded value
 */
public static BigDecimal round(BigDecimal value, int scale) {
    return value.setScale(scale, RoundingMode.HALF_UP);
}

From source file:ec.edu.distri.clientejava.protocolo.model.Canal.java

public void setCosto(BigDecimal costo) {
    this.costo = costo.setScale(2, RoundingMode.HALF_UP);
}

From source file:org.cryptomath.util.NumberUtil.java

public static BigDecimal truncate(final String text, final int pTimes) {
    BigDecimal bigDecimal = new BigDecimal(text);
    if (bigDecimal.scale() > pTimes) {
        bigDecimal = new BigDecimal(text).setScale(pTimes, RoundingMode.HALF_UP);
    }/*from  w  ww  .  j  av a 2 s  . com*/
    return bigDecimal.stripTrailingZeros();
}

From source file:Main.java

private JFormattedTextField setFormat(JFormattedTextField jft, Locale local1, int minFra, int maxFra) {
    NumberFormat numberFormat;//ww w.  ja  v  a2  s .  com
    Locale local = local1;
    int setMin = minFra;
    int setMax = maxFra;
    numberFormat = NumberFormat.getCurrencyInstance(local);
    numberFormat.setMinimumFractionDigits(setMin);
    numberFormat.setMaximumFractionDigits(setMax);
    numberFormat.setRoundingMode(RoundingMode.HALF_UP);
    jft = new JFormattedTextField(numberFormat);
    jft.setValue(new Double(342.796));
    return jft;
}

From source file:net.vexelon.bgrates.Utils.java

public static String roundNumber(BigDecimal number, int n) {
    return number.round(new MathContext(n, RoundingMode.HALF_UP)).toPlainString();
}