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:Main.java

public static void main(String[] args) {
    double operation = 890.0 / 1440.0;
    BigDecimal big = new BigDecimal(operation);
    big = big.setScale(4, RoundingMode.HALF_UP);
    double d2 = big.doubleValue();
    System.out.println(String.format("operation : %s", operation));
    System.out.println(String.format("scaled : %s", d2));
}

From source file:BigDecimalInvoiceApp.java

public static void main(String[] args) {
    double subtotal = 123.123;

    double discountPercent = 0.2;
    BigDecimal decimalSubtotal = new BigDecimal(Double.toString(subtotal));
    decimalSubtotal = decimalSubtotal.setScale(2, RoundingMode.HALF_UP);
    BigDecimal decimalDiscountPercent = new BigDecimal(Double.toString(discountPercent));

    BigDecimal discountAmount = decimalSubtotal.multiply(decimalDiscountPercent);
    discountAmount = discountAmount.setScale(2, RoundingMode.HALF_UP);

    BigDecimal totalBeforeTax = decimalSubtotal.subtract(discountAmount);
    BigDecimal salesTaxPercent = new BigDecimal(".05");
    BigDecimal salesTax = salesTaxPercent.multiply(totalBeforeTax);
    salesTax = salesTax.setScale(2, RoundingMode.HALF_UP);
    BigDecimal total = totalBeforeTax.add(salesTax);

    NumberFormat currency = NumberFormat.getCurrencyInstance();
    NumberFormat percent = NumberFormat.getPercentInstance();

    String message = "Subtotal:         " + currency.format(decimalSubtotal) + "\n" + "Discount percent: "
            + percent.format(decimalDiscountPercent) + "\n" + "Discount amount:  "
            + currency.format(discountAmount) + "\n" + "Total before tax: " + currency.format(totalBeforeTax)
            + "\n" + "Sales tax:        " + currency.format(salesTax) + "\n" + "Invoice total:    "
            + currency.format(total) + "\n";

    System.out.println(message);/* ww  w.  j  a v a2s . co  m*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(2);//from w w  w  . j  ava2s  .  c  o m
    format.setMinimumFractionDigits(2);
    format.setParseIntegerOnly(true);
    format.setRoundingMode(RoundingMode.HALF_UP);

    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMaximum(1000);
    formatter.setMinimum(0.0);
    formatter.setAllowsInvalid(false);
    // formatter.setOverwriteMode(false);

    JFormattedTextField tf = new JFormattedTextField(formatter);
    tf.setColumns(10);
    tf.setValue(123456789.99);
    JFormattedTextField tf1 = new JFormattedTextField(formatter);
    tf1.setValue(1234567890.99);
    JFormattedTextField tf2 = new JFormattedTextField(formatter);
    tf2.setValue(1111.1111);
    JFormattedTextField tf3 = new JFormattedTextField(formatter);
    tf3.setValue(-1111.1111);
    JFormattedTextField tf4 = new JFormattedTextField(formatter);
    tf4.setValue(-56);

    JFrame frame = new JFrame("Test");
    frame.setLayout(new GridLayout(5, 0));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(tf);
    frame.add(tf1);
    frame.add(tf2);
    frame.add(tf3);
    frame.add(tf4);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
    textField1.setFormatterFactory(new AbstractFormatterFactory() {
        @Override/*from ww w.  j ava  2  s .  co  m*/
        public AbstractFormatter getFormatter(JFormattedTextField tf) {
            NumberFormat format = DecimalFormat.getInstance();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);
            format.setRoundingMode(RoundingMode.HALF_UP);
            InternationalFormatter formatter = new InternationalFormatter(format);
            formatter.setAllowsInvalid(false);
            formatter.setMinimum(0.0);
            formatter.setMaximum(1000.00);
            return formatter;
        }
    });
    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setRoundingMode(RoundingMode.HALF_UP);
    final JFormattedTextField textField2 = new JFormattedTextField(numberFormat);
    textField2.setValue(new Float(10.01));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.SOUTH);
    frame.setVisible(true);
    frame.pack();
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
    textField1.setFormatterFactory(new AbstractFormatterFactory() {
        @Override/*from  w  ww  .j a va2 s . c o  m*/
        public AbstractFormatter getFormatter(JFormattedTextField tf) {
            NumberFormat format = DecimalFormat.getInstance();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);
            format.setRoundingMode(RoundingMode.HALF_UP);
            InternationalFormatter formatter = new InternationalFormatter(format);
            formatter.setAllowsInvalid(false);
            formatter.setMinimum(0.0);
            formatter.setMaximum(1000.00);
            return formatter;
        }
    });
    Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
    attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
    final JFormattedTextField textField2 = new JFormattedTextField(new Float(10.01));
    textField2.setFormatterFactory(new AbstractFormatterFactory() {

        @Override
        public AbstractFormatter getFormatter(JFormattedTextField tf) {
            NumberFormat format = DecimalFormat.getInstance();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);
            format.setRoundingMode(RoundingMode.HALF_UP);
            InternationalFormatter formatter = new InternationalFormatter(format);
            formatter.setAllowsInvalid(false);
            formatter.setMinimum(0.0);
            formatter.setMaximum(1000.00);
            return formatter;
        }
    });
    textField2.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        @Override
        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        @Override
        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            double t1a1 = (((Number) textField2.getValue()).doubleValue());
            if (t1a1 > 100) {
                textField2.setFont(new Font(attributes));
                textField2.setForeground(Color.red);
            } else {
                textField2.setFont(new Font("Serif", Font.BOLD, 16));
                textField2.setForeground(Color.black);
            }
        }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.SOUTH);
    frame.setVisible(true);
    frame.pack();
}

From source file:invoice.GetInvoice.java

/**
 * @param args the command line arguments
 */// w ww  . j  a v a 2  s . co m
public static void main(String[] args) {

    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);
    nf.setRoundingMode(RoundingMode.HALF_UP);

    try {
        JSONObject arg_json = new JSONObject(args[0]);
    } catch (JSONException ex) {
        Logger.getLogger(GetInvoice.class.getName()).log(Level.SEVERE, null, ex);
    }
    HashMap<String, Object> hm = new HashMap<>();
    hm.put("duplicate", "");
    hm.put("distributor", "//oshan" + "\n" + "//kapuhempala" + "\n\nArea: " + "//galle");
    hm.put("customer", "//owner" + "\n" + "//Agro" + "\n" + "//Agro add" + "\n" + "//0771894851");
    hm.put("invNo", "GSLTS" + String.format("%04d", Integer.parseInt("//100")));
    hm.put("invDate", "2014-01-10");
    hm.put("invCode", "300");

    double invoiceTotal = 500000;
    if (5 > 0) {//ShopDiscount
        double discountprice = (invoiceTotal * 99) / 100;//getShopDiscount()
        hm.put("invoiceDiscount", nf.format((invoiceTotal) * 99 / 100));//getRetail_discount()

    } else {
        hm.put("invoiceDiscount", "");
    }
    hm.put("gross_total", nf.format(invoiceTotal));
    hm.put("invoiceTotal", nf.format(((invoiceTotal) * (100 - 99) / 100)));//getRetail_discount()
    hm.put("salesPersonName", "rep");
    hm.put("salesPersonContactNo", "0772189584");

    JTable jTable1 = new JTable();
    jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {},
            new String[] { "ITEMCODE", "DESCRIPTION", "QTY", "FREEQTY", "PRICE", "AMOUNT" }));
    String reportSource = "./ireports/invoice.jrxml";
    DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
    try {
        JasperReport jr = JasperCompileManager.compileReport(reportSource);
        JasperPrint jp = JasperFillManager.fillReport(jr, hm, new JRTableModelDataSource(dtm));
        JasperPrintManager.printReport(jp, false);
    } catch (JRException ex) {
        Logger.getLogger(GetInvoice.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("1");
}

From source file:uk.ac.leeds.ccg.andyt.generic.visualisation.charts.Generic_ScatterPlotAndLinearRegression.java

public static void main(String[] args) {
    Generic_Visualisation.getHeadlessEnvironment();
    /*/*from  w  ww.  j a v  a  2 s . c  o  m*/
     * Initialise title and File to write image to
     */
    String title;
    File file;
    String format = "PNG";
    if (args.length != 2) {
        System.out.println("Expected 2 args:" + " args[0] title;" + " args[1] File." + " Recieved "
                + args.length + " args.");
        // Use defaults
        title = "Scatter Plot And Linear Regression";
        System.out.println("Use default title: " + title);
        file = new File(new File(System.getProperty("user.dir")), title.replace(" ", "_") + "." + format);
        System.out.println("Use default File: " + file.toString());
    } else {
        title = args[0];
        file = new File(args[1]);
    }
    int dataWidth = 256;//250;
    int dataHeight = 256;
    String xAxisLabel = "Expected (X)";
    String yAxisLabel = "Observed (Y)";
    boolean drawOriginLinesOnPlot = false;//true;
    int decimalPlacePrecisionForCalculations = 100;
    int decimalPlacePrecisionForDisplay = 3;
    RoundingMode aRoundingMode = RoundingMode.HALF_UP;
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Generic_ScatterPlotAndLinearRegression plot = new Generic_ScatterPlotAndLinearRegression(executorService,
            file, format, title, dataWidth, dataHeight, xAxisLabel, yAxisLabel, drawOriginLinesOnPlot,
            decimalPlacePrecisionForCalculations, decimalPlacePrecisionForDisplay, aRoundingMode);
    plot.run();
}

From source file:Main.java

public static int getBigDecimalInt(double value) {
    return BigDecimal.valueOf(value).setScale(0, RoundingMode.HALF_UP).intValue();
}

From source file:Main.java

public static double Rounding(double d) {
    BigDecimal bd = new BigDecimal(d);
    bd.setScale(1, RoundingMode.HALF_UP);
    return bd.floatValue();
}

From source file:Main.java

public static String roundUptoTwoDecimalUnits(String value) {
    return String.valueOf(new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue());
}