Example usage for java.text NumberFormat setMaximumFractionDigits

List of usage examples for java.text NumberFormat setMaximumFractionDigits

Introduction

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

Prototype

public void setMaximumFractionDigits(int newValue) 

Source Link

Document

Sets the maximum number of digits allowed in the fraction portion of a number.

Usage

From source file:canreg.client.analysis.Tools.java

public static JFreeChart generateJChart(Collection<CancerCasesCount> casesCounts, String fileName,
        String header, FileTypes fileType, ChartType chartType, boolean includeOther, boolean legendOn,
        Double restCount, Double allCount, Color color, String labelsCategoryName) {
    JFreeChart chart;//from  ww  w.j av a2s. c  om
    if (chartType == ChartType.PIE) {
        NumberFormat format = NumberFormat.getInstance();
        format.setMaximumFractionDigits(1);
        DefaultPieDataset dataset = new DefaultKeyedValuesDataset();
        int position = 0;
        for (CancerCasesCount count : casesCounts) {
            dataset.insertValue(position++,
                    count.toString() + " (" + format.format(count.getCount() / allCount * 100) + "%)",
                    count.getCount());
        }
        if (includeOther) {
            dataset.insertValue(position++,
                    "Other: " + restCount.intValue() + " (" + format.format(restCount / allCount * 100) + "%)",
                    restCount);
        }
        chart = ChartFactory.createPieChart(header, dataset, legendOn, false, Locale.getDefault());
        Tools.setPiePlotColours(chart, casesCounts.size() + 1, color.brighter());

    } else { // assume barchart
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (CancerCasesCount count : casesCounts) {
            dataset.addValue(count.getCount(), count.getLabel(), count.toString());
        }
        if (includeOther) {
            dataset.addValue(restCount.intValue(), "Other", "Other: " + restCount);
        }
        chart = ChartFactory.createStackedBarChart(header, labelsCategoryName, "Cases", dataset,
                PlotOrientation.HORIZONTAL, legendOn, true, false);

        Tools.setBarPlotColours(chart, casesCounts.size() + 1, color.brighter());
    }
    return chart;
}

From source file:org.totschnig.myexpenses.util.Utils.java

static String formatCurrency(BigDecimal amount, Currency currency) {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    int fractionDigits = currency.getDefaultFractionDigits();
    nf.setCurrency(currency);//from w ww  .  j  ava2  s .co m
    nf.setMinimumFractionDigits(fractionDigits);
    nf.setMaximumFractionDigits(fractionDigits);
    return nf.format(amount);
}

From source file:org.orbisgis.corejdbc.ReadTable.java

/**
 * Return a concatened and human readable format of provided result set
 * @param rs result set to read//from w  ww . jav a 2  s. co m
 * @param maxFieldLength Maximum field length to print
 * @param maxPrintedRows Maximum printed rows
 * @param addColumns Add column header
 * @param alignColumns Align columns by using padding
 * @param resultSetFilter Accept or refuse rows by implementing this interface
 * @return human readable format of provided result set
 * @throws SQLException
 */
public static String resultSetToString(ResultSet rs, int maxFieldLength, int maxPrintedRows, boolean addColumns,
        boolean alignColumns, ResultSetFilter resultSetFilter) throws SQLException {
    // Print headers
    ResultSetMetaData metaData = rs.getMetaData();
    int columnCount = metaData.getColumnCount();
    StringBuilder lines = new StringBuilder();
    StringBuilder formatStringBuilder = new StringBuilder();
    String[] header = new String[columnCount];
    for (int idColumn = 1; idColumn <= columnCount; idColumn++) {
        header[idColumn - 1] = metaData.getColumnLabel(idColumn) + "(" + metaData.getColumnTypeName(idColumn)
                + ")";
        if (alignColumns) {
            formatStringBuilder.append("%-");
            formatStringBuilder.append(maxFieldLength);
            formatStringBuilder.append("s ");
        } else {
            formatStringBuilder.append("%s ");
        }
    }
    if (addColumns) {
        lines.append(String.format(formatStringBuilder.toString(), header));
        lines.append("\n");
    }
    int shownLines = 0;
    NumberFormat decimalFormat = NumberFormat.getInstance(Locale.getDefault());
    decimalFormat.setGroupingUsed(false);
    decimalFormat.setMaximumFractionDigits(16);
    while (rs.next() && shownLines < maxPrintedRows) {
        if (resultSetFilter.printRow(rs)) {
            String[] row = new String[columnCount];
            for (int idColumn = 1; idColumn <= columnCount; idColumn++) {
                Object valObj = rs.getObject(idColumn);
                String value;
                if (valObj instanceof Number) {
                    value = decimalFormat.format(valObj);
                } else {
                    value = rs.getString(idColumn);
                }
                if (value != null) {
                    if (columnCount > 1 && value.length() > maxFieldLength) {
                        value = value.substring(0, maxFieldLength - 2) + "..";
                    }
                } else {
                    value = "NULL";
                }
                row[idColumn - 1] = value;
            }
            shownLines++;
            lines.append(String.format(formatStringBuilder.toString(), row));
            lines.append("\n");
        }
    }
    if (lines.length() != 0) {
        return lines.toString();
    } else {
        return I18N.tr("No attributes to show");
    }
}

From source file:SystemUtils.java

public static void PrintMemoryUsage(String memMesg) {
    StringBuffer sb = new StringBuffer();
    double availableMem;
    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(true);/*from  ww w. j  a v  a2 s  . c  o m*/
    nf.setMaximumFractionDigits(3);

    availableMem = GetAvailableMemory();

    sb.append(memMesg);

    sb.append(nf.format(availableMem));
    sb.append(" MB");

    System.out.println(sb.toString());
}

From source file:com.cubusmail.mail.util.MessageUtils.java

/**
 * @param locale/*w ww  .j  av a 2s  .c  om*/
 * @return
 */
public static NumberFormat createSizeFormat(Locale locale) {

    NumberFormat sizeFormat = DecimalFormat.getNumberInstance(locale);
    sizeFormat.setGroupingUsed(true);
    sizeFormat.setMaximumFractionDigits(0);
    sizeFormat.setMinimumFractionDigits(0);

    return sizeFormat;
}

From source file:com.icloud.framework.http.heritrix.ArchiveUtils.java

private static String doubleToString(double val, int maxFractionDigits, int minFractionDigits) {
    NumberFormat f = NumberFormat.getNumberInstance(Locale.US);
    f.setMaximumFractionDigits(maxFractionDigits);
    f.setMinimumFractionDigits(minFractionDigits);
    return f.format(val);
}

From source file:org.yawlfoundation.yawl.util.StringUtil.java

/**
 * Utility routine which takes a decimal value as a string (e.g. 0.25 equating to 25p) and returns the
 * value in UI currency format (e.g. 0.25).
 *
 * @return A formatted currency// w ww.java  2 s . c o m
 */
public static String formatDecimalCost(BigDecimal value) {
    Currency currency = Currency.getInstance(Locale.getDefault());
    NumberFormat fmt = DecimalFormat.getInstance();
    fmt.setMinimumFractionDigits(2);
    fmt.setMaximumFractionDigits(2);
    return currency.getSymbol() + fmt.format(value);
}

From source file:com.nridge.core.base.std.StrUtl.java

/**
 * Formats the size (in bytes) in a human readable string of
 * bytes, KB, MB, GB and TB./*from  w  w  w.  j a  va2 s  . c  o  m*/
 *
 * @param aSizeInBytes Size in bytes.
 *
 * @return Formatted string.
 */
public static String bytesToString(long aSizeInBytes) {
    NumberFormat numberFormat = new DecimalFormat();
    numberFormat.setMaximumFractionDigits(2);

    try {
        if (aSizeInBytes < SIZE_IN_KB)
            return numberFormat.format(aSizeInBytes) + " Byte(s)";
        else if (aSizeInBytes < SIZE_IN_MB)
            return numberFormat.format(aSizeInBytes / SIZE_IN_KB) + " KB";
        else if (aSizeInBytes < SIZE_IN_GB)
            return numberFormat.format(aSizeInBytes / SIZE_IN_MB) + " MB";
        else if (aSizeInBytes < SIZE_IN_TB)
            return numberFormat.format(aSizeInBytes / SIZE_IN_GB) + " GB";
        else
            return numberFormat.format(aSizeInBytes / SIZE_IN_TB) + " TB";
    } catch (Exception e) {
        return aSizeInBytes + " Byte(s)";
    }
}

From source file:org.rhq.core.util.StringUtil.java

public static String formatDuration(long duration, int scale, boolean minDigits) {
    long hours;/*from  w  ww . j a va 2  s  .  c o m*/
    long mins;
    int digits;
    double millis;

    hours = duration / 3600000;
    duration -= hours * 3600000;

    mins = duration / 60000;
    duration -= mins * 60000;

    millis = (double) duration / 1000;

    StringBuilder buf = new StringBuilder();

    if ((hours > 0) || (minDigits == false)) {
        buf.append(((hours < 10) && (minDigits == false)) ? ("0" + hours) : String.valueOf(hours)).append(':');
        minDigits = false;
    }

    if ((mins > 0) || (minDigits == false)) {
        buf.append(((mins < 10) && (minDigits == false)) ? ("0" + mins) : String.valueOf(mins)).append(':');
        minDigits = false;
    }

    // Format seconds and milliseconds
    NumberFormat fmt = NumberFormat.getInstance();
    digits = (((minDigits == false) || ((scale == 0) && (millis >= 9.5))) ? 2 : 1);
    fmt.setMinimumIntegerDigits(digits);
    fmt.setMaximumIntegerDigits(2); // Max of 2
    fmt.setMinimumFractionDigits(0); // Don't need any
    fmt.setMaximumFractionDigits(scale);

    buf.append(fmt.format(millis));

    return buf.toString();
}

From source file:org.renjin.parser.NumericLiterals.java

public static NumberFormat createRealFormat() {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMinimumFractionDigits(0);//  w  w w  .j  a  va 2 s  . c o  m
    format.setMaximumFractionDigits(14);
    format.setGroupingUsed(false);
    return format;
}