Example usage for java.text DecimalFormat setMinimumFractionDigits

List of usage examples for java.text DecimalFormat setMinimumFractionDigits

Introduction

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

Prototype

@Override
public void setMinimumFractionDigits(int newValue) 

Source Link

Document

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

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>//  www .  j  av  a  2 s .co  m
 * <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:org.apache.sysml.runtime.util.DataConverter.java

/**
 * Returns a string representation of a matrix
 * @param mb matrix block/*  w  ww. j  a  v  a2  s  .  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);/*from   w w w.j av  a  2s.  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: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  ww. 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();
}

From source file:org.opensingular.form.wicket.mapper.DecimalMapper.java

private String formatDecimal(BigDecimal bigDecimal, boolean groupingUsed) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat.getInstance(new Locale("pt", "BR"));
    nf.setParseBigDecimal(true);/*  ww  w.ja  v  a2 s .co  m*/
    nf.setGroupingUsed(groupingUsed);
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(bigDecimal.scale());
    return nf.format(bigDecimal);
}

From source file:no.abmu.questionnaire.domain.data.BigDecimalFieldData.java

@Transient
private DecimalFormat getNumberFormat() {
    Locale locale = LocaleTypeNameConst.BOKMAAL;
    DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getInstance(locale);
    numberFormat.setMinimumFractionDigits(bigDecimalScale);
    numberFormat.setParseBigDecimal(true);
    return numberFormat;
}

From source file:org.opensingular.form.wicket.mapper.MoneyMapper.java

private String formatDecimal(BigDecimal bigDecimal, Integer casasDecimais) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat.getInstance(new Locale("pt", "BR"));
    nf.setParseBigDecimal(true);//  ww w .  j  av  a  2s  .c o  m
    nf.setGroupingUsed(true);
    nf.setMinimumFractionDigits(casasDecimais);
    nf.setMaximumFractionDigits(casasDecimais);
    return nf.format(bigDecimal);
}

From source file:org.openehr.rm.datatypes.quantity.DvQuantity.java

/**
 * string form displayable for humans/*from   w w  w.j a  va  2 s  .c  om*/
 *
 * @return string presentation
 */
public String toString() {
    DecimalFormat format = new DecimalFormat();
    format.setMinimumFractionDigits(precision);
    format.setMaximumFractionDigits(precision);
    DecimalFormatSymbols dfs = format.getDecimalFormatSymbols();
    dfs.setDecimalSeparator(DECIMAL_SEPARATOR);
    format.setDecimalFormatSymbols(dfs);
    format.setGroupingUsed(false);
    String tmp = format.format(magnitude) + (StringUtils.isEmpty(getUnits()) ? "" : "," + getUnits());
    return tmp;
}

From source file:org.mitre.math.linear.RandomMatrixExample.java

/**
 * /*from   w w  w  .  j a va 2 s.  c  o  m*/
 * @param m
 * @param width      Column width.
 * @param digits     Number of digits after the decimal.
 * @return
 */
public String matrix2String(RealMatrix m, int width, int digits) {
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(digits);
    format.setMinimumFractionDigits(digits);
    format.setGroupingUsed(false);

    StringBuilder output = new StringBuilder();
    output.append('\n'); // start on new line.
    for (int i = 0; i < m.getRowDimension(); i++) {
        for (int j = 0; j < m.getColumnDimension(); j++) {
            String s = format.format(m.getEntry(i, j)); // format the number
            int padding = Math.max(1, width - s.length()); // At _least_ 1 space
            for (int k = 0; k < padding; k++) {
                output.append(' ');
            }
            output.append(s);
        }
        output.append('\n');
    }
    output.append('\n'); // end with blank line.
    return output.toString();
}

From source file:com.blackbear.flatworm.converters.CoreConverters.java

public String convertBigDecimal(Object obj, Map<String, ConversionOption> options) {
    if (obj == null) {
        return null;
    }//w  w w. j a  v  a2  s  . co m
    BigDecimal bd = (BigDecimal) obj;

    int decimalPlaces = 0;
    String decimalPlacesOption = (String) Util.getValue(options, "decimal-places");
    boolean decimalImplied = "true".equals(Util.getValue(options, "decimal-implied"));

    if (decimalPlacesOption != null)
        decimalPlaces = Integer.parseInt(decimalPlacesOption);

    DecimalFormat format = new DecimalFormat();
    format.setDecimalSeparatorAlwaysShown(!decimalImplied);
    format.setMinimumFractionDigits(decimalPlaces);
    format.setMaximumFractionDigits(decimalPlaces);

    return format.format(bd.doubleValue());
}