Example usage for java.text DecimalFormat setGroupingUsed

List of usage examples for java.text DecimalFormat setGroupingUsed

Introduction

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

Prototype

@Override
public void setGroupingUsed(boolean newValue) 

Source Link

Usage

From source file:com.att.aro.core.util.Util.java

/**
 * Formats a number so that the number of digits in the fraction
 * portion of it is bound by a maximum value and a minimum value.
 * <br>/*from ww  w  .j a v  a2  s  . c  om*/
 * <br>
 * Examples with maxFractionDigits being 3 and
 * minFractionDigits being 0:
 * <br>2.4535 -> 2.454
 * <br>20 -> 20
 * <br>24535 -> 24535
 * <br>2.5 -> 2.5
 * <br>2.460 -> 2.46
 * <br>2.40 -> 2.4
 * <br>3.12 -> 3.12
 * <br>9.888 -> 9.888
 *
 * @param number
 * @param maxFractionDigits maximum number of fraction digits,
 * replaced by 0 if it is a negative value
 * @param minFractionDigits minimum number of fraction digits,
 * replaced by 0 if it is a negative value
 * @return
 */
public static String formatDecimal(BigDecimal number, int maxFractionDigits, int minFractionDigits) {
    DecimalFormat df = new DecimalFormat();
    df.setGroupingUsed(false);
    df.setMaximumFractionDigits(maxFractionDigits);
    df.setMinimumFractionDigits(minFractionDigits);
    return df.format(number);
}

From source file:com.itude.mobile.mobbl.core.util.StringUtilities.java

private static void setupFormatter(DecimalFormat formatter, int minumumDecimals, int maximumDecimals) {
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(getDefaultFormattingLocale()));
    formatter.setMinimumIntegerDigits(1);

    if (minumumDecimals > -1) {
        formatter.setMinimumFractionDigits(minumumDecimals);
    }/*  ww w . j a v a  2 s  .co  m*/
    if (maximumDecimals > -1) {
        formatter.setMaximumFractionDigits(maximumDecimals);
    }
    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
}

From source file:org.apache.sysml.runtime.util.DataConverter.java

/**
 * Returns a string representation of a matrix
 * @param mb matrix block//from   w w  w . j  a va2s  .c o m
 * @param sparse if true, string will contain a table with row index, col index, value (where value != 0.0)
 *              otherwise it will be a rectangular string with all values of the matrix block
 * @param separator Separator string between each element in a row, or between the columns in sparse format
 * @param lineseparator Separator string between each row
 * @param rowsToPrint maximum number of rows to print, -1 for all
 * @param colsToPrint maximum number of columns to print, -1 for all
 * @param decimal number of decimal places to print, -1 for default
 * @return matrix as a string
 */
public static String toString(MatrixBlock mb, boolean sparse, String separator, String lineseparator,
        int rowsToPrint, int colsToPrint, int decimal) {
    StringBuffer sb = new StringBuffer();

    // Setup number of rows and columns to print
    int rlen = mb.getNumRows();
    int clen = mb.getNumColumns();
    int rowLength = rlen;
    int colLength = clen;
    if (rowsToPrint >= 0)
        rowLength = rowsToPrint < rlen ? rowsToPrint : rlen;
    if (colsToPrint >= 0)
        colLength = colsToPrint < clen ? colsToPrint : clen;

    DecimalFormat df = new DecimalFormat();
    df.setGroupingUsed(false);
    if (decimal >= 0) {
        df.setMinimumFractionDigits(decimal);
    }

    if (sparse) { // Sparse Print Format
        if (mb.isInSparseFormat()) { // Block is in sparse format
            Iterator<IJV> sbi = mb.getSparseBlockIterator();
            while (sbi.hasNext()) {
                IJV ijv = sbi.next();
                int row = ijv.getI();
                int col = ijv.getJ();
                double value = ijv.getV();
                if (row < rowLength && col < colLength) {
                    // Print (row+1) and (col+1) since for a DML user, everything is 1-indexed
                    sb.append(row + 1).append(separator).append(col + 1).append(separator);
                    sb.append(df.format(value)).append(lineseparator);
                }
            }
        } else { // Block is in dense format
            for (int i = 0; i < rowLength; i++) {
                for (int j = 0; j < colLength; j++) {
                    double value = mb.getValue(i, j);
                    if (value != 0.0) {
                        sb.append(i + 1).append(separator).append(j + 1).append(separator);
                        sb.append(df.format(value)).append(lineseparator);
                    }
                }
            }
        }
    } else { // Dense Print Format
        for (int i = 0; i < rowLength; i++) {
            for (int j = 0; j < colLength - 1; j++) {
                double value = mb.quickGetValue(i, j);
                sb.append(df.format(value));
                sb.append(separator);
            }
            double value = mb.quickGetValue(i, colLength - 1);
            sb.append(df.format(value)); // Do not put separator after last element
            sb.append(lineseparator);
        }
    }

    return sb.toString();
}

From source file:org.apache.sysml.runtime.util.DataConverter.java

public static String toString(FrameBlock fb, boolean sparse, String separator, String lineseparator,
        int rowsToPrint, int colsToPrint, int decimal) {
    StringBuffer sb = new StringBuffer();

    // Setup number of rows and columns to print
    int rlen = fb.getNumRows();
    int clen = fb.getNumColumns();
    int rowLength = rlen;
    int colLength = clen;
    if (rowsToPrint >= 0)
        rowLength = rowsToPrint < rlen ? rowsToPrint : rlen;
    if (colsToPrint >= 0)
        colLength = colsToPrint < clen ? colsToPrint : clen;

    //print frame header
    sb.append("# FRAME: ");
    sb.append("nrow = " + fb.getNumRows() + ", ");
    sb.append("ncol = " + fb.getNumColumns() + lineseparator);

    //print column names
    sb.append("#");
    sb.append(separator);/* w w  w  .j a  va 2 s.com*/
    for (int j = 0; j < colLength; j++) {
        sb.append(fb.getColumnNames()[j]);
        if (j != colLength - 1)
            sb.append(separator);
    }
    sb.append(lineseparator);

    //print schema
    sb.append("#");
    sb.append(separator);
    for (int j = 0; j < colLength; j++) {
        sb.append(fb.getSchema()[j]);
        if (j != colLength - 1)
            sb.append(separator);
    }
    sb.append(lineseparator);

    //print data
    DecimalFormat df = new DecimalFormat();
    df.setGroupingUsed(false);
    if (decimal >= 0)
        df.setMinimumFractionDigits(decimal);

    Iterator<Object[]> iter = fb.getObjectRowIterator(0, rowLength);
    while (iter.hasNext()) {
        Object[] row = iter.next();
        for (int j = 0; j < colLength; j++) {
            if (row[j] != null) {
                if (fb.getSchema()[j] == ValueType.DOUBLE)
                    sb.append(df.format(row[j]));
                else
                    sb.append(row[j]);
                if (j != colLength - 1)
                    sb.append(separator);
            }
        }
        sb.append(lineseparator);
    }

    return sb.toString();
}

From source file:org.eclairjs.nashorn.Utils.java

public static String formatDouble(Double v) {
    try {//from ww  w .  j  a  v  a  2s. co m
        DecimalFormat df = new DecimalFormat();
        df.setGroupingUsed(false);
        df.setMaximumFractionDigits(12);
        return df.format(v);
    } catch (java.lang.IllegalArgumentException e) {
        return v.toString();
    }
}

From source file:com.itude.mobile.android.util.StringUtil.java

/**
 * Set the formatter./*from w  w  w . j  a  va  2s.  c o  m*/
 * 
 * @param formatter {@link DecimalFormat}
 * @param locale {@link Locale}
 * @param minimalDecimalNumbers minimal number of decimals
 * @param maximumDecimalNumbers maximum number of decimals
 */
private static void setupFormatter(DecimalFormat formatter, Locale locale, int minimalDecimalNumbers,
        int maximumDecimalNumbers) {
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
    formatter.setMinimumIntegerDigits(1);
    formatter.setMinimumFractionDigits(minimalDecimalNumbers);
    formatter.setMaximumFractionDigits(maximumDecimalNumbers);
    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
}

From source file:com.jaspersoft.jasperserver.war.cascade.handlers.converters.DoubleDataConverter.java

@Override
public String valueToString(Double value) {
    if (value == null)
        return "";
    DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(LocaleContextHolder.getLocale());
    df.setGroupingUsed(false);
    df.setMaximumFractionDigits(Integer.MAX_VALUE);
    return df.format(value);
}

From source file:com.jaspersoft.jasperserver.war.cascade.handlers.converters.FloatDataConverter.java

@Override
public String valueToString(Float value) {
    if (value == null)
        return "";
    DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(LocaleContextHolder.getLocale());
    df.setGroupingUsed(false);
    df.setMaximumFractionDigits(Integer.MAX_VALUE);
    return df.format(value);
}

From source file:whitelabel.cloud.adapter.CurrencyFormatterExt.java

public NumberFormat getNumberFormat(Locale locale, String pattern) {

    NumberFormat df = NumberFormat.getNumberInstance(locale);
    DecimalFormat dec = (DecimalFormat) df;
    dec.applyPattern(pattern);//from   w ww .ja  v a2 s  .c  om
    dec.setMaximumFractionDigits(2);
    dec.setGroupingUsed(false);
    return dec;
}

From source file:edu.byu.nlp.util.Matrices.java

public static String toString(double[][] mat, int maxRows, int maxCols, int numFractionDigits) {
    // how many digits are in the biggest number in each column?
    int[] maxColNumWidths = Matrices.argMaxesInColumns(mat);
    for (int c = 0; c < maxColNumWidths.length; c++) {
        maxColNumWidths[c] = strWidthOf(mat[maxColNumWidths[c]][c], numFractionDigits);
    }//from w  w  w  . j  a v  a  2  s .  co m
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(numFractionDigits);
    df.setMinimumFractionDigits(numFractionDigits);
    df.setGroupingUsed(false);

    StringBuilder str = new StringBuilder();
    str.append('[');
    int numrows = Math.min(mat.length, maxRows);
    for (int r = 0; r < numrows; r++) {
        if (r > 0) {
            str.append("\n ");
        }
        str.append('[');
        for (int c = 0; c < Math.min(mat[r].length, maxCols); c++) {
            if (c > 0) {
                str.append(", ");
            }
            int strWidth = strWidthOf(mat[r][c], numFractionDigits);
            for (int fill = strWidth; fill < maxColNumWidths[c]; fill++) {
                str.append(' ');
            }
            str.append(df.format(mat[r][c]));
        }
        if (mat[r].length > maxCols) {
            str.append(", ...");
        }
        str.append("]");
        if (r < numrows - 1) {
            str.append(",");
        }
    }
    if (mat.length > maxRows) {
        str.append(",\n  ...\n");
    }
    str.append("]");
    return str.toString();
}