Example usage for java.text NumberFormat format

List of usage examples for java.text NumberFormat format

Introduction

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

Prototype

public final String format(long number) 

Source Link

Document

Specialization of format.

Usage

From source file:fr.ericlab.mabed.structure.EventList.java

public void exportDetailledResults(Corpus corpus) {
    File outputDir = new File("output/csv/");
    if (!outputDir.isDirectory()) {
        outputDir.mkdir();/*from ww w  . jav  a2 s.  c  o  m*/
    } else {
        for (String filename : outputDir.list()) {
            FileUtils.deleteQuietly(new File("output/csv/" + filename));
        }
    }
    NumberFormat formatter = new DecimalFormat("000");
    for (int i = 0; i < list.size(); i++) {
        Event event = list.get(i);
        String mainTerm = event.mainTerm.replace(", ", "_");
        File descFile = new File("output/csv/" + formatter.format(i) + "-" + mainTerm + ".desc");
        File wordsFile = new File("output/csv/" + formatter.format(i) + "-" + mainTerm + ".words");
        File seriesFile = new File("output/csv/" + formatter.format(i) + "-" + mainTerm + ".anomaly");
        try {
            FileUtils.writeStringToFile(descFile, event.score + "\t" + event.I.timeSliceA + "\t"
                    + event.I.timeSliceB + "\t"
                    + new SimpleDateFormat("YYYY-MM-dd HH:mm").format(corpus.toDate(event.I.timeSliceA)) + "\t"
                    + new SimpleDateFormat("YYYY-MM-dd HH:mm").format(corpus.toDate(event.I.timeSliceB)) + "\n",
                    true);
        } catch (IOException ex) {
            Logger.getLogger(EventList.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (WeightedTerm wt : event.relatedTerms.list) {
            try {
                FileUtils.writeStringToFile(wordsFile, wt.term + "\t" + wt.weight + "\n", true);
            } catch (IOException ex) {
                Logger.getLogger(EventList.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        for (int j = 0; j < event.anomaly.size(); j++) {
            try {
                FileUtils.writeStringToFile(seriesFile, j + "\t" + event.anomaly.get(j) + "\n", true);
            } catch (IOException ex) {
                Logger.getLogger(EventList.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:com.trenako.entities.Money.java

@Override
public String toString() {
    if (this == EMPTY_VALUE) {
        return "-";
    }//w  ww .  j  a va 2s .  co  m

    BigDecimal v = BigDecimal.valueOf(getValue()).divide(MONEY_VALUE_FACTOR);

    NumberFormat nf = NumberFormat.getCurrencyInstance();

    // the user may have selected a different currency than the default
    Currency currency = Currency.getInstance(getCurrency());
    nf.setCurrency(currency);
    nf.setMinimumFractionDigits(2);

    return nf.format(v);
}

From source file:org.gephi.statistics.plugin.Degree.java

public String getDirectedReport() {
    //Distribution series
    XYSeries dSeries = ChartUtils.createXYSeries(degreeDist, "Degree Distribution");
    XYSeries idSeries = ChartUtils.createXYSeries(inDegreeDist, "In-Degree Distribution");
    XYSeries odSeries = ChartUtils.createXYSeries(outDegreeDist, "Out-Degree Distribution");

    XYSeriesCollection dataset1 = new XYSeriesCollection();
    dataset1.addSeries(dSeries);//ww  w . j  a  v  a2  s  .  c  o  m

    XYSeriesCollection dataset2 = new XYSeriesCollection();
    dataset2.addSeries(idSeries);

    XYSeriesCollection dataset3 = new XYSeriesCollection();
    dataset3.addSeries(odSeries);

    JFreeChart chart1 = ChartFactory.createXYLineChart("Degree Distribution", "Value", "Count", dataset1,
            PlotOrientation.VERTICAL, true, false, false);
    chart1.removeLegend();
    ChartUtils.decorateChart(chart1);
    ChartUtils.scaleChart(chart1, dSeries, false);
    String degreeImageFile = ChartUtils.renderChart(chart1, "degree-distribution.png");

    JFreeChart chart2 = ChartFactory.createXYLineChart("In-Degree Distribution", "Value", "Count", dataset2,
            PlotOrientation.VERTICAL, true, false, false);
    chart2.removeLegend();
    ChartUtils.decorateChart(chart2);
    ChartUtils.scaleChart(chart2, dSeries, false);
    String indegreeImageFile = ChartUtils.renderChart(chart2, "indegree-distribution.png");

    JFreeChart chart3 = ChartFactory.createXYLineChart("Out-Degree Distribution", "Value", "Count", dataset3,
            PlotOrientation.VERTICAL, true, false, false);
    chart3.removeLegend();
    ChartUtils.decorateChart(chart3);
    ChartUtils.scaleChart(chart3, dSeries, false);
    String outdegreeImageFile = ChartUtils.renderChart(chart3, "outdegree-distribution.png");

    NumberFormat f = new DecimalFormat("#0.000");

    String report = "<HTML> <BODY> <h1>Degree Report </h1> " + "<hr>" + "<br> <h2> Results: </h2>"
            + "Average Degree: " + f.format(avgDegree) + "<br /><br />" + degreeImageFile + "<br /><br />"
            + indegreeImageFile + "<br /><br />" + outdegreeImageFile + "</BODY></HTML>";

    return report;
}

From source file:fr.ericlab.mabed.structure.Corpus.java

public String getMessages(Event event) {
    String messages = "";
    NumberFormat formatter = new DecimalFormat("00000000");
    String mainTerm = event.mainTerm;
    int count = 0;
    for (int i = event.I.timeSliceA; i <= event.I.timeSliceB; i++) {
        try {/*from  w  ww  .j  a va  2s .c o  m*/
            String filename = "input/" + formatter.format(i) + ".text";
            List<String> lines = FileUtils.readLines(new File(filename));
            for (String line : lines) {
                if (line.contains(" " + mainTerm + " ")) {
                    messages += line + "\n";
                    count++;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return messages;
}

From source file:com.aurel.track.attachment.AttachBL.java

/**
 * @return name of the attachment on disk
 *//*from ww  w . java2s.  c  om*/
public static String getFileNameAttachment(Integer attachID, Integer itemID) {
    NumberFormat nf = new DecimalFormat("00000");
    String dbId = nf.format(attachID);
    // choose different suffix for attachments of new WorkItems
    String fileSuffix;
    if (itemID == null || itemID.intValue() == -1) {
        fileSuffix = AttachDbFnameTypeNew;
    } else {
        fileSuffix = AttachDbFnameType;
    }
    return AttachDbFname + dbId + fileSuffix;
}

From source file:com.adito.networkplaces.model.FileSystemItem.java

private String formatSize(long bytes) {

    NumberFormat formatMb = NumberFormat.getNumberInstance();
    NumberFormat formatGb = NumberFormat.getNumberInstance();
    NumberFormat formatKb = NumberFormat.getNumberInstance();

    if ((bytes / 1099511627776L) > 0) {
        // Were in the gigabytes
        return formatSizeDPS(formatGb.format((double) bytes / 1099511627776L)) + " GB";
    } else if ((bytes / 1048576) > 0) {
        // Were in the megabytes
        return formatSizeDPS(formatMb.format((double) bytes / 1048576)) + " MB";
    } else {/* w w  w .  ja  v a2 s  .c  om*/
        // Were still in Kilobytes
        return formatSizeDPS(formatKb.format((double) bytes / 1024)) + " KB";
    }
}

From source file:com.codebutler.farebot.transit.suica.SuicaTrip.java

@Override
public String getBalanceString() {
    NumberFormat format = NumberFormat.getCurrencyInstance(Locale.JAPAN);
    format.setMaximumFractionDigits(0);/*from  w ww. ja v a  2  s. c  o m*/
    return format.format(mBalance);
}

From source file:de.tor.tribes.util.bb.BasicFormatter.java

protected void formatElementsCore(final StringBuilder builder, final Collection<C> elems,
        final boolean pExtended, final String listItemTemplate, final NumberFormat format) {
    int cnt = 1;/*from w ww.  j av a  2  s . co  m*/
    for (C n : elems) {
        String[] replacements = n.getReplacements(pExtended);
        String itemLine = StringUtils.replaceEach(listItemTemplate, n.getBBVariables(), replacements);
        itemLine = StringUtils.replaceEach(itemLine, new String[] { ELEMENT_ID, ELEMENT_COUNT },
                new String[] { format.format(cnt), format.format(elems.size()) });
        builder.append(itemLine).append("\n");
        cnt++;
    }
}

From source file:de.xwic.appkit.webbase.table.DefaultColumnLabelProvider.java

private String toString(Object value) {
    String text;/* w  w  w  .  ja v  a 2  s  . c o  m*/

    if (value == null) {
        text = "";
    } else if (value instanceof Date) {
        // special treatment for date objects
        text = dateFormatter.format((Date) value);
    } else if (value instanceof Calendar) {
        Calendar c = (Calendar) value;
        text = dateFormatter.format(c.getTime());
    } else if (value instanceof Long) {
        NumberFormat nf = NumberFormat.getNumberInstance(locale);
        text = nf.format(((Long) value).longValue());
    } else if (value instanceof Double) {
        NumberFormat nf = NumberFormat.getNumberInstance(locale);
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        text = nf.format(((Double) value).doubleValue());
    } else if (propertyEditor != null) {
        propertyEditor.setValue(value);
        text = propertyEditor.getAsText();
    } else if (value instanceof IPicklistEntry) {
        text = ((IPicklistEntry) value).getBezeichnung(locale.getLanguage());
    } else if (value instanceof IEntity) {
        IEntity entity = (IEntity) value;
        DAO<?> dao = DAOSystem.findDAOforEntity(entity.type());
        text = dao.buildTitle(entity);
    } else if (value instanceof Collection<?>) {
        // set of picklist entries
        StringBuffer sb = new StringBuffer();
        for (Iterator<?> it = ((Collection<?>) value).iterator(); it.hasNext();) {
            Object o = it.next();
            if (sb.length() != 0) {
                sb.append(",");
            }
            sb.append(toString(o));
        }
        text = sb.toString();
    } else if (value instanceof Object[]) {
        StringBuffer sb = new StringBuffer();
        Object[] values = (Object[]) value;
        for (int i = 0; i < values.length; i++) {
            if (sb.length() != 0) {
                sb.append(",");
            }
            sb.append(toString(values[i]));
        }
        text = sb.toString();
    } else {
        text = value.toString();
    }
    return text;

}

From source file:org.pentaho.plugin.jfreereport.reportcharts.ScalingLogarithmicAxis.java

protected List refreshTicksVertical(final Graphics2D g2, final Rectangle2D dataArea, final RectangleEdge edge) {

    final ArrayList ticks = new ArrayList();

    // get lower bound value:
    double lowerBoundVal = getRange().getLowerBound();
    // if small log values and lower bound value too small
    // then set to a small value (don't allow <= 0):
    if (this.smallLogFlag && lowerBoundVal < SMALL_LOG_VALUE) {
        lowerBoundVal = SMALL_LOG_VALUE;
    }/*  w  w  w.  j  a  va  2s.  co m*/
    // get upper bound value
    final double upperBoundVal = getRange().getUpperBound();

    // get log10 version of lower bound and round to integer:
    int iBegCount = (int) Math.rint(switchedLog10(lowerBoundVal));
    // get log10 version of upper bound and round to integer:
    final int iEndCount = (int) Math.rint(switchedLog10(upperBoundVal));

    if (iBegCount == iEndCount && iBegCount > 0 && Math.pow(10, iBegCount) > lowerBoundVal) {
        // only 1 power of 10 value, it's > 0 and its resulting
        // tick value will be larger than lower bound of data
        --iBegCount; // decrement to generate more ticks
    }

    double tickVal;
    String tickLabel;
    boolean zeroTickFlag = false;
    final int[] intervals = { 10, 10, 10, 10, 10, 5, 5, 5, 3, 3 };

    for (int i = iBegCount; i <= iEndCount; i++) {
        // for each tick with a label to be displayed
        int jEndCount = getTicksVertical();
        if (jEndCount == -1) {
            jEndCount = Math.abs(iEndCount - iBegCount) < 10 ? intervals[iEndCount - iBegCount] : 1;
        }

        if (i == iEndCount) {
            jEndCount = 1;
        }

        for (int j = 0; j < jEndCount; j++) {
            // for each tick to be displayed
            if (this.smallLogFlag) {
                // small log values in use
                tickVal = Math.pow(10, i) + (Math.pow(10, i) * j);
                if (j == 0) {
                    // first tick of group; create label text
                    if (this.log10TickLabelsFlag) {
                        // if flag then
                        tickLabel = "10^" + i; // create "log10"-type label
                    } else { // not "log10"-type label
                        if (this.expTickLabelsFlag) {
                            // if flag then
                            tickLabel = "1e" + i; // create "1e#"-type label
                        } else { // not "1e#"-type label
                            if (i >= 0) { // if positive exponent then
                                // make integer
                                final NumberFormat format = getNumberFormatOverride();
                                if (format != null) {
                                    tickLabel = format.format(tickVal);
                                } else {
                                    tickLabel = LogCategoryItemLabelGenerator.formatValue(new Double(tickVal));
                                    /*tickLabel = Long.toString((long) Math.rint(tickVal));*/
                                }
                            } else {
                                // negative exponent; create fractional value
                                // set exact number of fractional digits to
                                // be shown:
                                this.numberFormatterObj.setMaximumFractionDigits(-i);
                                // create tick label:
                                tickLabel = this.numberFormatterObj.format(tickVal);
                            }
                        }
                    }
                } else { // not first tick to be displayed
                    tickLabel = ""; // no tick label
                }
            } else { // not small log values in use; allow for values <= 0
                if (zeroTickFlag) { // if did zero tick last iter then
                    --j;
                } // decrement to do 1.0 tick now
                tickVal = (i >= 0) ? Math.pow(10, i) + (Math.pow(10, i) * j)
                        : j == 0 ? -Math.pow(10, -i) : -(Math.pow(10, -i) - (Math.pow(10, -i - 1) * (9 - j)));
                if (j == 0) { // first tick of group
                    if (!zeroTickFlag) { // did not do zero tick last
                        // iteration
                        if (i > iBegCount && i < iEndCount && Math.abs(tickVal - 1.0) < 0.0001) {
                            // not first or last tick on graph and value
                            // is 1.0
                            tickVal = 0.0; // change value to 0.0
                            zeroTickFlag = true; // indicate zero tick
                            tickLabel = "0"; // create label for tick
                        } else {
                            // first or last tick on graph or value is 1.0
                            // create label for tick:
                            if (this.log10TickLabelsFlag) {
                                // create "log10"-type label
                                tickLabel = (((i < 0) ? "-" : "") + "10^" + Math.abs(i));
                            } else {
                                if (this.expTickLabelsFlag) {
                                    // create "1e#"-type label
                                    tickLabel = (((i < 0) ? "-" : "") + "1e" + Math.abs(i));
                                } else {
                                    final NumberFormat format = getNumberFormatOverride();
                                    if (format != null) {
                                        tickLabel = format.format(tickVal);
                                    } else {
                                        tickLabel = LogCategoryItemLabelGenerator.formatValue(new Double(
                                                tickVal));/*
                                                          tickLabel =  Long.toString((long) Math.rint(tickVal));*/
                                    }
                                }
                            }
                        }
                    } else { // did zero tick last iteration
                        tickLabel = ""; // no label
                        zeroTickFlag = false; // clear flag
                    }
                } else { // not first tick of group
                    tickLabel = ""; // no label
                    zeroTickFlag = false; // make sure flag cleared
                }
            }

            if (tickVal > upperBoundVal) {
                return ticks; // if past highest data value then exit method
            }

            if (tickVal >= lowerBoundVal - SMALL_LOG_VALUE) {
                // tick value not below lowest data value
                final TextAnchor anchor;
                final TextAnchor rotationAnchor;
                double angle = 0.0;
                if (isVerticalTickLabels()) {
                    if (edge == RectangleEdge.LEFT) {
                        anchor = TextAnchor.BOTTOM_CENTER;
                        rotationAnchor = TextAnchor.BOTTOM_CENTER;
                        angle = -Math.PI / 2.0;
                    } else {
                        anchor = TextAnchor.BOTTOM_CENTER;
                        rotationAnchor = TextAnchor.BOTTOM_CENTER;
                        angle = Math.PI / 2.0;
                    }
                } else {
                    if (edge == RectangleEdge.LEFT) {
                        anchor = TextAnchor.CENTER_RIGHT;
                        rotationAnchor = TextAnchor.CENTER_RIGHT;
                    } else {
                        anchor = TextAnchor.CENTER_LEFT;
                        rotationAnchor = TextAnchor.CENTER_LEFT;
                    }
                }

                ticks.add(new NumberTick(new Double(tickVal), tickLabel, anchor, rotationAnchor, angle));
            }
        }
    }
    return ticks;
}