Example usage for java.text DecimalFormat DecimalFormat

List of usage examples for java.text DecimalFormat DecimalFormat

Introduction

In this page you can find the example usage for java.text DecimalFormat DecimalFormat.

Prototype

public DecimalFormat(String pattern) 

Source Link

Document

Creates a DecimalFormat using the given pattern and the symbols for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:net.sf.dynamicreports.design.transformation.chartcustomizer.ShowValuesCustomizer.java

@Override
public void customize(JFreeChart chart, ReportParameters reportParameters) {
    if (chart.getPlot() instanceof CategoryPlot) {
        CategoryItemRenderer renderer = chart.getCategoryPlot().getRenderer();
        if (StringUtils.isBlank(valuePattern)) {
            renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        } else {//from w ww  .j a  v a2s. c om
            renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(
                    StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING,
                    new DecimalFormat(valuePattern)));
        }
        renderer.setBaseItemLabelsVisible(Boolean.TRUE);
        chart.getCategoryPlot().getRangeAxis().zoomRange(0, 1.1);
        if (renderer.getClass().equals(BarRenderer3D.class)) {
            ((BarRenderer3D) renderer).setItemLabelAnchorOffset(10D);
            renderer.setBasePositiveItemLabelPosition(
                    new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
        }
    } else if (chart.getPlot() instanceof XYPlot) {
        XYItemRenderer renderer = chart.getXYPlot().getRenderer();
        if (StringUtils.isBlank(valuePattern)) {
            renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
        } else {
            renderer.setBaseItemLabelGenerator(
                    new StandardXYItemLabelGenerator(StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT,
                            NumberFormat.getNumberInstance(), new DecimalFormat(valuePattern)));
        }
        renderer.setBaseItemLabelsVisible(Boolean.TRUE);
        chart.getXYPlot().getRangeAxis().zoomRange(0, 1.1);
    }
}

From source file:com.wabacus.system.datatype.FloatType.java

public String value2label(Object value) {
    if (value == null)
        return "";
    if (!(value instanceof Float))
        return String.valueOf(value);
    if (this.numberformat != null && !this.numberformat.trim().equals("")) {
        DecimalFormat df = new DecimalFormat(this.numberformat);
        return df.format((Float) value);
    } else {/*  w w w  .  jav  a  2  s . c  o  m*/
        return String.valueOf(value);
    }
}

From source file:com.wabacus.system.datatype.DoubleType.java

public String value2label(Object value) {
    if (value == null)
        return "";
    if (!(value instanceof Double))
        return String.valueOf(value);
    if (this.numberformat != null && !this.numberformat.trim().equals("")) {
        DecimalFormat df = new DecimalFormat(this.numberformat);
        return df.format((Double) value);
    } else {/*from  w w w .j  a  v a 2 s.  c o m*/
        return String.valueOf(value);
    }
}

From source file:com.strider.datadefender.DataDefender.java

private static void displayExecutionTime(final long startTime) {
    final long endTime = System.currentTimeMillis();
    final NumberFormat formatter = new DecimalFormat("#0.00000");

    LOG.info("Execution time is " + formatter.format((endTime - startTime) / 1000d) + " seconds");
    LOG.info("DataDefender completed ");
}

From source file:Unicode.java

/** Construct the object including its GUI */
public Unicode() {
    super("Unicode");

    Container cp = getContentPane();

    // Used both for Buttons and Menus
    ResourceBundle b = ResourceBundle.getBundle("UnicodeWidgets");

    JButton quitButton, nextButton, prevButton;
    Panel p = new Panel();
    // Make a grid, add one for labels.
    p.setLayout(new GridLayout(ROWS + 1, COLUMNS + 1));
    DecimalFormat df2d = new DecimalFormat("00");

    // Add first row, just column labels.
    p.add(new JLabel(""));
    for (int i = 0; i < COLUMNS; i++)
        p.add(new JLabel(Integer.toString(i, 16), JLabel.CENTER));

    // Add subsequent rows, each with an offset label
    for (int i = 0; i < ROWS; i++) {
        JLabel l = new JLabel("0000"); // room for max, i.e. \uFFFF
        p.add(l);/* w w w .ja v a  2s .c o m*/
        rowLabs[i] = l;
        for (int j = 0; j < COLUMNS; j++) {
            JLabel pb = new JLabel(" ");
            buttons[j][i] = pb;
            p.add(pb);
        }
    }

    // ActionListeners for jumping around; used by buttons and menus
    ActionListener firster = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            gotoPage(startNum = 0);
        }
    };
    ActionListener previouser = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (startNum > 0)
                gotoPage(startNum -= QUADSIZE);
        }
    };
    ActionListener nexter = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (startNum < 65535)
                gotoPage(startNum += QUADSIZE);
        }
    };
    ActionListener laster = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            gotoPage(65536 - QUADSIZE);
        }
    };

    cp.add(BorderLayout.NORTH, p);
    fontName = new JLabel("Default font", JLabel.CENTER);
    cp.add(BorderLayout.CENTER, fontName);
    Panel q = new Panel();
    cp.add(BorderLayout.SOUTH, q);
    q.add(prevButton = mkButton(b, "page.prev"));
    prevButton.addActionListener(previouser);

    q.add(nextButton = mkButton(b, "page.next"));
    nextButton.addActionListener(nexter);

    q.add(quitButton = mkButton(b, "exit"));
    quitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            dispose();
            System.exit(0);
        }
    });
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            setVisible(false);
            dispose();
            System.exit(0);
        }
    });

    MenuItem mi; // used in various spots

    MenuBar mb = new MenuBar();
    setMenuBar(mb);

    String titlebar;
    try {
        titlebar = b.getString("program" + ".title");
    } catch (MissingResourceException e) {
        titlebar = "Unicode Demo";
    }
    setTitle(titlebar);

    ActionListener fontSelector = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String font = e.getActionCommand();
            mySetFont(font, FONTSIZE);
        }
    };

    Menu fontMenu = mkMenu(b, "font");
    // String[] fontList = Toolkit.getDefaultToolkit().getFontList();
    String[] fontList = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    for (int i = 0; i < fontList.length; i++) {
        fontMenu.add(mi = new MenuItem(fontList[i]));
        mi.addActionListener(fontSelector);
    }
    mb.add(fontMenu);

    gotoPageUI = new GoToPage("Unicode Page");
    centre(gotoPageUI);

    Menu vm = mkMenu(b, "page");
    vm.add(mi = mkMenuItem(b, "page", "first"));
    mi.addActionListener(firster);
    vm.add(mi = mkMenuItem(b, "page", "prev"));
    mi.addActionListener(previouser);
    vm.add(mi = mkMenuItem(b, "page", "next"));
    mi.addActionListener(nexter);
    vm.add(mi = mkMenuItem(b, "page", "last"));
    mi.addActionListener(laster);
    vm.add(mi = mkMenuItem(b, "page", "goto"));
    mi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Unicode.this.gotoPageUI.setVisible(true);
        }
    });
    mb.add(vm);

    Menu hm = mkMenu(b, "help");
    hm.add(mi = mkMenuItem(b, "help", "about"));
    mb.setHelpMenu(hm); // needed for portability (Motif, etc.).

    pack();
    // After packing the Frame, centre it on the screen.
    centre(this);

    // start at a known place
    mySetFont(fontList[0], FONTSIZE);
    gotoPage(startNum);
}

From source file:aos.lucene.search.advanced.SortingExample.java

public void displayResults(Query query, Sort sort) //
        throws IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    searcher.setDefaultFieldSortScoring(true, false); //

    TopDocs results = searcher.search(query, null, //
            20, sort); //

    LOGGER.info("\nResults for: " + //
            query.toString() + " sorted by " + sort);

    LOGGER.info(StringUtils.rightPad("Title", 30) + StringUtils.rightPad("pubmonth", 10)
            + StringUtils.center("id", 4) + StringUtils.center("score", 15));

    PrintStream out = new PrintStream(System.out, true, "UTF-8"); //

    DecimalFormat scoreFormatter = new DecimalFormat("0.######");
    for (ScoreDoc sd : results.scoreDocs) {
        int docID = sd.doc;
        float score = sd.score;
        Document doc = searcher.doc(docID);
        out.println(StringUtils.rightPad( //
                StringUtils.abbreviate(doc.get("title"), 29), 30) + //
                StringUtils.rightPad(doc.get("pubmonth"), 10) + //
                StringUtils.center("" + docID, 4) + //
                StringUtils.leftPad( //
                        scoreFormatter.format(score), 12)); //
        out.println("   " + doc.get("category"));
        //out.println(searcher.explain(query, docID));   //
    }/* w ww .  j  a  v a  2  s  .  co m*/

    searcher.close();
}

From source file:com.saiton.ccs.validations.FormatAndValidate.java

public String roundTwoDecimals(double d) {
    DecimalFormat twoDForm = new DecimalFormat("0.00");
    twoDForm.setGroupingUsed(false);//from   ww w .  j a va2 s  . c o  m
    return twoDForm.format(d);
}

From source file:io.hops.hopsworks.common.util.FormatUtils.java

public static String timeInSec(Long t) {
    DecimalFormat format = new DecimalFormat("#.#");
    if (t == null) {
        return "";
    }//from   w  w w.j a  v a2 s .  c  om
    Double time = (double) t;
    if (time < m) {
        return format.format(time) + "s";
    }
    if (time < h) {
        return format.format(time / m) + "m";
    }
    if (time < d) {
        return format.format(time / h) + "h";
    }
    return format.format(time / d) + "d";
}

From source file:com.fatwire.dta.sscrawler.reporting.reporters.NestingReporter.java

@Override
public void endCollecting() {
    report.startReport();/*from w w  w  .j  a  v  a 2s . c o  m*/
    report.addHeader("threshold", Integer.toString(threshold));
    report.addHeader("uri", "nesting");

    for (final QueryString qs : tracker.getKeys()) {
        if (qs instanceof Link) {
            final int level = tracker.getNestingLevel(qs);
            total.addValue(level);
            if (level >= threshold) {
                count.incrementAndGet();

                report.addRow(qs.toString(), Integer.toString(level));
            } else if (level == 0) {
                report.addRow(qs.toString(), Integer.toString(level));
            }
        }
    }
    final DecimalFormat df = new DecimalFormat("###0.00");
    report.addRow("Average Nesting level", df.format(total.getMean()));
    report.addRow("Maximum Nesting level", Long.toString(Math.round(total.getMax())));
    report.addRow("Minimum Nesting level", Long.toString(Math.round(total.getMin())));
    super.endCollecting();
}

From source file:com.wuliu.biz.orderdetail.engine.impl.OrderDetailMergeEngineImpl.java

@Override
public List<WuliuMergedOrderDetailModel> mergeOrderDetail(List<WuliuOrderDetailModel> wuliuOrderDetailModels,
        Long weightPrice, Long volumnPrice) {
    List<WuliuMergedOrderDetailModel> ret = new ArrayList<WuliuMergedOrderDetailModel>();

    if (CollectionUtils.isEmpty(wuliuOrderDetailModels)) {
        return ret;
    }/*from w  w  w . ja v  a2 s . c o  m*/

    WuliuMergedOrderDetailModel weightModel = null;
    WuliuMergedOrderDetailModel volumnModel = null;

    for (WuliuOrderDetailModel item : wuliuOrderDetailModels) {
        Long weightCost = getWeightCost(item, weightPrice);
        Long volumnCost = getVolumnCost(item, volumnPrice);
        if (weightCost * 1000000 > volumnCost) {
            if (weightModel == null) {
                weightModel = new WuliuMergedOrderDetailModel();
                weightModel.setType(WuliuMergedOrderDetailConst.TYPE_WEIGHT);
            }

            add(weightModel, item.getWeight(), 0L, weightCost, item.getCount());
        } else {
            if (volumnModel == null) {
                volumnModel = new WuliuMergedOrderDetailModel();
                volumnModel.setType(WuliuMergedOrderDetailConst.TYPE_VOLUMN);
            }

            add(volumnModel, 0, getVolumn(item), volumnCost, item.getCount());
        }
    }

    if (weightModel != null) {
        if (weightModel.getCost() % 100000L == 0) {
            weightModel.setCost(weightModel.getCost() / 100000);
        } else {
            weightModel.setCost((long) Math.ceil(weightModel.getCost() / 100000.0));
        }

        DecimalFormat df = new DecimalFormat("0.#");
        if (weightModel.getWeight() > 0) {
            weightModel.setWeightForDisplay(df.format(weightModel.getWeight() / 1000.0));
        }

        ret.add(weightModel);
    }

    if (volumnModel != null) {
        if (volumnModel.getCost() % 100000000000L == 0) {
            volumnModel.setCost(volumnModel.getCost() / 100000000000L);
        } else {
            volumnModel.setCost((long) Math.ceil(volumnModel.getCost() / 100000000000.0f));
        }
        ret.add(volumnModel);

        DecimalFormat df = new DecimalFormat("0.###");
        if (volumnModel.getVolumn() > 0) {
            volumnModel.setVolumnForDisplay(df.format(Math.ceil(volumnModel.getVolumn() / 1000000.0) / 1000.0));
        }
    }

    return ret;
}