Example usage for java.text DecimalFormat setMaximumFractionDigits

List of usage examples for java.text DecimalFormat setMaximumFractionDigits

Introduction

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

Prototype

@Override
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:Main.java

public static String formatNumber(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);//  ww w.j  a va 2  s.c  om
    return df.format(amount);
}

From source file:Main.java

public static String convertToDecimal(String val) {

    String convertedValue = "";

    try {/*www .java 2s.  co  m*/
        DecimalFormat df = new DecimalFormat("0.00");
        convertedValue = df.format(Double.parseDouble(val));
        df.setMaximumFractionDigits(2);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }

    return convertedValue;
}

From source file:Main.java

public static DecimalFormat createDecimalFormat(int minInt, int maxInt, int minFract, int maxFract,
        char separator, RoundingMode mode) {

    DecimalFormat format = (DecimalFormat) DecimalFormat.getNumberInstance();
    format.setRoundingMode(mode);//from   w w  w.  j  a  v a 2 s  . c  o  m

    format.setMaximumFractionDigits(maxFract);
    format.setMinimumFractionDigits(minFract);
    format.setMaximumIntegerDigits(maxInt);
    format.setMinimumIntegerDigits(minInt);
    DecimalFormatSymbols decimalSymbolComma = new DecimalFormatSymbols();
    decimalSymbolComma.setDecimalSeparator(separator);
    format.setDecimalFormatSymbols(decimalSymbolComma);
    format.setGroupingUsed(false);

    return format;
}

From source file:rsa_matrices.Helpers.java

public static void printMatrix(double[][] matrix, String matrixName) {
    int MAXIMUM_ALLOWED_COUNT = 10;
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);//from www.j a  v a  2  s . co  m

    System.out.println(matrixName);
    for (int row = 0; row < matrix.length && row < MAXIMUM_ALLOWED_COUNT; row++) {
        for (int col = 0; col < matrix[0].length && col < MAXIMUM_ALLOWED_COUNT; col++) {
            System.out.print("  " + df.format(matrix[row][col]));
        }
        if (matrix[0].length > MAXIMUM_ALLOWED_COUNT) {
            System.out.print(" ... ");
        }
        System.out.println();
    }
    if (matrix.length > MAXIMUM_ALLOWED_COUNT) {
        System.out.print("  ... ... ... ");
    }
    System.out.println();
}

From source file:pl.otros.vfs.browser.table.FileSize.java

private static String format(final long value, final long divider, final String unit) {
    final double result = divider > 1 ? (double) value / (double) divider : (double) value;
    DecimalFormat decimalFormat = new DecimalFormat();
    decimalFormat.setMaximumFractionDigits(1);
    decimalFormat.setMinimumFractionDigits(0);
    decimalFormat.setGroupingUsed(false);
    decimalFormat.setDecimalSeparatorAlwaysShown(false);
    return decimalFormat.format(result) + " " + unit;
}

From source file:com.midisheetmusicmemo.ChooseSongActivity.java

public static void logHeap() {
    Double allocated = new Double(Debug.getNativeHeapAllocatedSize()) / new Double((1048576));
    Double available = new Double(Debug.getNativeHeapSize()) / 1048576.0f;
    Double free = new Double(Debug.getNativeHeapFreeSize()) / 1048576.0f;
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);//from www.  j  a  v a2  s.c  om

    Log.d("blah", "debug. =================================");
    Log.d("blah", "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available)
            + "MB (" + df.format(free) + "MB free)");
    Log.d("blah",
            "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory() / 1048576))
                    + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory() / 1048576)) + "MB ("
                    + df.format(new Double(Runtime.getRuntime().freeMemory() / 1048576)) + "MB free)");
    System.gc();
    System.gc();
}

From source file:org.toobsframework.transformpipeline.xslExtentions.PriceFormatHelper.java

/**
 * Gets a string that represents the input Price formatted into the proper fromate, and converted
 * into the proper timezone./*from ww  w  .jav  a 2  s. co  m*/
 *
 * @return Price-only string formatted with given time zone.
 *
 * @exception XMLTransfromerException if parsing problem occurs
 */
public static String getFormattedPrice(String inputPrice, String priceFormat, String language)
        throws XMLTransformerException {
    if ((inputPrice == null) || (inputPrice.trim().length() == 0)) {
        inputPrice = "0";
    }

    Locale locale = new Locale(language.substring(2, 4).toLowerCase(), language.substring(0, 2));
    DecimalFormat priceFormatter = (DecimalFormat) NumberFormat.getNumberInstance(locale);
    priceFormatter.setGroupingUsed(true);
    priceFormatter.setMaximumFractionDigits(2);
    priceFormatter.setMinimumFractionDigits(2);

    priceFormatter.applyPattern(priceFormat);

    return priceFormatter.format(new Double(inputPrice));
}

From source file:com.streamreduce.util.MessageUtils.java

public static String roundAndTruncate(double rawValue, int precision) {
    DecimalFormat df = new DecimalFormat();

    df.setMaximumFractionDigits(precision);
    df.setMinimumFractionDigits(precision);
    df.setMinimumIntegerDigits(1);/*from   w  w w.j  a v  a 2s  .com*/

    return df.format(rawValue);
}

From source file:com.tinypace.mobistore.util.StringUtil.java

public static String formatMoney(BigDecimal d) {
    DecimalFormat mformat = new DecimalFormat();
    mformat.setMaximumFractionDigits(2);
    mformat.setMinimumFractionDigits(2);
    mformat.setGroupingSize(20);//  w  ww .j a v a 2s .  c  o m
    return mformat.format(d);
}

From source file:ai.grakn.graql.internal.util.StringConverter.java

/**
 * @param value a value in the graph//from ww  w  .j  av  a 2  s . com
 * @return the string representation of the value (using quotes if it is already a string)
 */
public static String valueToString(Object value) {
    if (value instanceof String) {
        return quoteString((String) value);
    } else if (value instanceof Double) {
        DecimalFormat df = new DecimalFormat("#", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
        df.setMinimumFractionDigits(1);
        df.setMaximumFractionDigits(12);
        df.setMinimumIntegerDigits(1);
        return df.format(value);
    } else {
        return value.toString();
    }
}