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:StringUtils.java

/**
 * Format a percentage for presentation to the user.
 * @param done the percentage to format (0.0 to 1.0)
 * @param digits the number of digits past the decimal point
 * @return a string representation of the percentage
 *//*from w  w w . j a va 2 s  .  c o  m*/
public static String formatPercent(double done, int digits) {
    DecimalFormat percentFormat = new DecimalFormat("0.00%");
    double scale = Math.pow(10.0, digits + 2);
    double rounded = Math.floor(done * scale);
    percentFormat.setDecimalSeparatorAlwaysShown(false);
    percentFormat.setMinimumFractionDigits(digits);
    percentFormat.setMaximumFractionDigits(digits);
    return percentFormat.format(rounded / scale);
}

From source file:com.micabyte.android.app.BaseActivity.java

@SuppressWarnings({ "rawtypes", "MagicNumber" })
public static void logHeap(Class clazz) {
    final DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);//  w w  w  . ja v  a 2s .c  o m
    if (BuildConfig.DEBUG)
        Log.d(clazz.getName(),
                "DEBUG_MEMORY allocated " + df.format((double) (Runtime.getRuntime().totalMemory() / 1048576))
                        + "/" + df.format((double) (Runtime.getRuntime().maxMemory() / 1048576)) + "MB ("
                        + df.format((double) (Runtime.getRuntime().freeMemory() / 1048576)) + "MB free)");
    System.gc();
    System.gc();
}

From source file:org.talend.dataprep.transformation.actions.math.ExtractNumber.java

/**
 * @param value the value to parse./*from  ww  w .  j  av a 2s.c  o m*/
 * @param defaultValue the value to return when no number can be extracted
 * @return the number extracted out of the given value.
 */
protected static String extractNumber(String value, String defaultValue) {

    // easy case
    if (StringUtils.isEmpty(value)) {
        return defaultValue;
    }

    // Test if the input value is a valid number before removing any characters:
    if (NumericHelper.isBigDecimal(value)) {
        // If yes (no exception thrown), return the value as it, no change required:
        return String.valueOf(BigDecimalParser.toBigDecimal(value));
    }

    StringCharacterIterator iter = new StringCharacterIterator(value);

    MetricPrefix metricPrefixBefore = null, metricPrefixAfter = null;

    boolean numberFound = false;

    // we build a new value including only number or separator as , or .
    StringBuilder reducedValue = new StringBuilder(value.length());

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        // we remove all non numeric characters but keep separators
        if (NumberUtils.isNumber(String.valueOf(c)) || SEPARATORS.contains(c)) {
            reducedValue.append(c);
            numberFound = true;
        } else {
            // we take the first metric prefix found before and after a number found
            if (metricPrefixBefore == null) {
                MetricPrefix found = METRICPREFIXES.get(String.valueOf(c));
                if (found != null && !numberFound) {
                    metricPrefixBefore = found;
                }
            }
            if (metricPrefixAfter == null) {
                MetricPrefix found = METRICPREFIXES.get(String.valueOf(c));
                if (found != null && numberFound) {
                    metricPrefixAfter = found;
                }
            }

        }
    }

    if (!NumericHelper.isBigDecimal(reducedValue.toString())) {
        return defaultValue;
    }
    BigDecimal bigDecimal = BigDecimalParser.toBigDecimal(reducedValue.toString());

    if (metricPrefixBefore != null || metricPrefixAfter != null) {
        // the metrix found after use first
        MetricPrefix metricPrefix = metricPrefixAfter != null ? metricPrefixAfter : metricPrefixBefore;
        bigDecimal = bigDecimal.multiply(metricPrefix.getMultiply());
    }

    DecimalFormat decimalFormat = new DecimalFormat("0.#");
    decimalFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS_DISPLAY);
    return decimalFormat.format(bigDecimal.stripTrailingZeros());
}

From source file:org.j2free.jsp.el.StandardExtensions.java

/**
 *
 * @param d//  w w w  .  java2  s  . c om
 * @return
 */
public static String formatPercent(float d) {

    DecimalFormat n = new DecimalFormat();
    n.setMinimumFractionDigits(2);
    n.setMaximumFractionDigits(2);
    return n.format(d);
}

From source file:org.j2free.jsp.el.StandardExtensions.java

/**
 *
 * @param decimal// ww w  .  j  a v a 2  s. c  om
 * @param min
 * @param max
 * @return
 */
public static String formatDecimal(double decimal, int min, int max) {
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(min);
    df.setMaximumFractionDigits(max);
    return df.format(decimal);
}

From source file:org.locationtech.udig.tools.internal.CursorPosition.java

private static String getString(double value) {
    if (Double.isNaN(value)) {
        return Messages.CursorPosition_not_a_number;
    }/*from   w  ww .  ja  v a 2s.c  o  m*/

    if (Double.isInfinite(value)) {
        return Messages.CursorPosition_infinity;
    }

    DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault());
    format.setMaximumFractionDigits(4);
    format.setMinimumIntegerDigits(1);
    format.setGroupingUsed(false);
    String string = format.format(value);

    String[] parts = string.split("\\.");
    if (parts.length > 3) {
        string = parts[0];
    }
    return string;
}

From source file:Main.java

public static String safeDoubleToCurrency(Double val) {
    BigDecimal value = BigDecimal.valueOf(val);
    DecimalFormat kursIndonesia = (DecimalFormat) DecimalFormat.getCurrencyInstance();
    DecimalFormatSymbols formatRp = new DecimalFormatSymbols();

    formatRp.setCurrencySymbol("");
    formatRp.setMonetaryDecimalSeparator('.');
    formatRp.setGroupingSeparator(',');

    kursIndonesia.setDecimalFormatSymbols(formatRp);
    kursIndonesia.setParseBigDecimal(true);
    if (val < 1)
        kursIndonesia.setMaximumFractionDigits(8);
    else if (val < 10)
        kursIndonesia.setMaximumFractionDigits(6);
    else if (val < 100)
        kursIndonesia.setMaximumFractionDigits(4);

    return kursIndonesia.format(value);
}

From source file:br.com.itfox.utils.Utils.java

public static String formatDecimal(BigDecimal val) {
    String s = "";
    try {// www.j  a  v  a 2s  . c om
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("en", "EN"));
        df.setMaximumFractionDigits(Preferences.MAXIMUM_FRACTION_DIGITS);
        df.setMinimumFractionDigits(Preferences.MINIMUM_FRACTION_DIGITS);
        df.setMinimumIntegerDigits(Preferences.MIMIMUM_INTEGER_DIGITS);
        df.setRoundingMode(Preferences.ROUNDING_MODE);
        s = String.valueOf(df.format(val));
    } catch (Exception ex) {
        br.com.itfox.utils.Logger.getLogger(ex, Utils.class.getName() + "***" + val + "***", ex.getMessage());
    }
    return s;

}

From source file:br.com.itfox.utils.Utils.java

public static String formatDecimal(float val) {
    String s = "";
    if (val > 0) {
        try {/* www . j a  va 2  s.  c  om*/
            DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("en", "EN"));
            df.setMaximumFractionDigits(Preferences.MAXIMUM_FRACTION_DIGITS);
            df.setMinimumFractionDigits(Preferences.MINIMUM_FRACTION_DIGITS);
            df.setMinimumIntegerDigits(Preferences.MIMIMUM_INTEGER_DIGITS);
            df.setRoundingMode(Preferences.ROUNDING_MODE);
            s = String.valueOf(df.format(val));
        } catch (Exception ex) {
            br.com.itfox.utils.Logger.getLogger(ex, Utils.class.getName() + "***" + val + "***",
                    ex.getMessage());
        }
    }
    return s;

}

From source file:br.com.itfox.utils.Utils.java

public static String formatDecimal(double val) {
    String s = "";
    if (val > 0) {
        try {/*  ww w. j  ava  2s  .co  m*/
            DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("en", "EN"));
            df.setMaximumFractionDigits(Preferences.MAXIMUM_FRACTION_DIGITS);
            df.setMinimumFractionDigits(Preferences.MINIMUM_FRACTION_DIGITS);
            df.setMinimumIntegerDigits(Preferences.MIMIMUM_INTEGER_DIGITS);
            df.setRoundingMode(Preferences.ROUNDING_MODE);
            s = String.valueOf(df.format(val));
        } catch (Exception ex) {
            br.com.itfox.utils.Logger.getLogger(ex, Utils.class.getName() + "***" + val + "***",
                    ex.getMessage());
        }
    }
    return s;

}