Example usage for java.text NumberFormat setMinimumFractionDigits

List of usage examples for java.text NumberFormat setMinimumFractionDigits

Introduction

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

Prototype

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.norbl.util.StringUtil.java

public static String f2(double x) {
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(2);//from   w w  w  . j av a 2  s .co m
    f.setMinimumFractionDigits(2);
    return (f.format(x));
}

From source file:org.kuali.ole.sys.context.Log4jConfigurer.java

protected static NumberFormat getNumberFormatter() {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(false);/*from   w w w.ja  v a  2 s. co  m*/
    nf.setMaximumFractionDigits(1);
    nf.setMinimumFractionDigits(1);
    return nf;
}

From source file:Main.java

private static JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() {
    JSpinner spinner = new JSpinner(new SpinnerNumberModel());
    JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
    JFormattedTextField textField = jsEditor.getTextField();
    DocumentFilter digitOnlyFilter = new DocumentFilter() {
        @Override//from   w  w w.j a v  a 2 s.  c  o m
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
                throws BadLocationException {
            if (stringContainsOnlyDigits(string)) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            super.remove(fb, offset, length);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
                throws BadLocationException {
            if (stringContainsOnlyDigits(text)) {
                super.replace(fb, offset, length, text, attrs);
            }
        }

        private boolean stringContainsOnlyDigits(String text) {
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    };

    NumberFormat format = NumberFormat.getPercentInstance();
    format.setGroupingUsed(false);
    format.setGroupingUsed(true);
    format.setMaximumIntegerDigits(10);
    format.setMaximumFractionDigits(2);
    format.setMinimumFractionDigits(5);
    textField.setFormatterFactory(new DefaultFormatterFactory(new InternationalFormatter(format) {
        @Override
        protected DocumentFilter getDocumentFilter() {
            return digitOnlyFilter;
        }
    }));
    return spinner;
}

From source file:com.skubit.iab.activities.TransactionDetailsActivity.java

private static final String formatCurrencyAmount(BigDecimal balanceNumber) {
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
    numberFormat.setMaximumFractionDigits(8);
    numberFormat.setMinimumFractionDigits(4);

    if (balanceNumber.compareTo(BigDecimal.ZERO) == -1) {
        balanceNumber = balanceNumber.multiply(new BigDecimal(-1));
    }//  w w  w  . j  a va  2  s . co  m

    return numberFormat.format(balanceNumber);
}

From source file:org.openestate.io.core.NumberUtils.java

/**
 * Convert a string value into a number.
 *
 * @param value//ww w.j  a  v  a 2s  . c  o m
 * the string value to convert
 *
 * @param integerOnly
 * wether only the integer part of the value is parsed
 *
 * @param locales
 * locales, against which the value is parsed
 *
 * @return
 * numeric value for the string
 *
 * @throws NumberFormatException
 * if the string is not a valid number
 */
public static Number parseNumber(String value, boolean integerOnly, Locale... locales)
        throws NumberFormatException {
    value = StringUtils.trimToNull(value);
    if (value == null)
        return null;

    // ignore leading plus sign
    if (value.startsWith("+"))
        value = StringUtils.trimToNull(value.substring(1));

    // remove any spaces
    value = StringUtils.replace(value, StringUtils.SPACE, StringUtils.EMPTY);

    if (ArrayUtils.isEmpty(locales))
        locales = new Locale[] { Locale.getDefault() };

    for (Locale locale : locales) {
        // check, if the value is completely numeric for the locale
        if (!isNumeric(value, locale))
            continue;
        try {
            NumberFormat format = NumberFormat.getNumberInstance(locale);
            format.setMinimumFractionDigits(0);
            format.setGroupingUsed(false);
            format.setParseIntegerOnly(integerOnly);
            return format.parse(value);
        } catch (ParseException ex) {
        }
    }
    throw new NumberFormatException("The provided value '" + value + "' is not numeric!");
}

From source file:net.nosleep.superanalyzer.util.Misc.java

private static String formatCount(double value, String unitSingular, String unitPlural, boolean showFraction) {
    NumberFormat numberFormat = NumberFormat.getInstance();
    if (showFraction == true) {
        numberFormat.setMinimumFractionDigits(0);
        numberFormat.setMaximumFractionDigits(1);
    }// ww  w .j  ava  2  s  .  c  om

    if (value > 1.0 && value < 1.01)
        return numberFormat.format(value) + " " + capitalizeByLocale(unitSingular);
    else
        return numberFormat.format(value) + " " + capitalizeByLocale(unitPlural);
}

From source file:com.SCI.centraltoko.utility.UtilityTools.java

public static String formatNumber(BigDecimal value) {
    if (value == null || value.equals(BigDecimal.ZERO)) {
        return "0";
    } else {//from   w  w  w.ja  v a 2 s  . c o m
        NumberFormat formater = NumberFormat.getInstance();
        formater.setMaximumFractionDigits(0);
        formater.setMinimumFractionDigits(0);
        return formater.format(value.setScale(0, RoundingMode.HALF_EVEN));
    }
}

From source file:org.openestate.io.core.NumberUtils.java

/**
 * Write a number into a string value./*from ww w  .j  av  a 2s. c  om*/
 *
 * @param value
 * the number to write
 *
 * @param integerDigits
 * maximal number of integer digits
 *
 * @param fractionDigits
 * maximal number of fraction digits
 *
 * @param locale
 * locale for decimal separator (using {@link Locale#ENGLISH} if null)
 *
 * @return
 * the formatted number
 */
public static String printNumber(Number value, int integerDigits, int fractionDigits, Locale locale) {
    if (value == null)
        return null;
    NumberFormat format = NumberFormat.getNumberInstance((locale != null) ? locale : Locale.ENGLISH);
    format.setMaximumIntegerDigits(integerDigits);
    format.setMaximumFractionDigits(fractionDigits);
    format.setMinimumFractionDigits(0);
    format.setGroupingUsed(false);
    return format.format(value);
}

From source file:de.tor.tribes.util.html.FightReportHTMLToolTipGenerator.java

public static String buildToolTip(FightReport pReport) {
    String res = pTemplateData;/*from   www. j  a va2s  .co  m*/
    String[] tables = buildUnitTables(pReport);
    SimpleDateFormat f = new SimpleDateFormat("dd.MM.yy HH:mm:ss");
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(0);
    res = res.replaceAll(WINNER_STRING,
            ((pReport.isWon() ? "Der Angreifer hat gewonnen" : "Der Verteidiger hat gewonnen")));
    res = res.replaceAll(SEND_TIME, f.format(new Date(pReport.getTimestamp())));
    res = res.replaceAll(ATTACKER_TABLE, tables[0]);
    res = res.replaceAll(DEFENDER_TABLE, tables[1]);
    res = res.replaceAll(MISC_TABLES, buildMiscTables(pReport));
    res = res.replaceAll(LUCK_STRING, "Gl&uuml;ck (aus Sicht des Angreifers)");
    res = res.replaceAll(LUCK_BAR, buildLuckBar(pReport.getLuck()));
    res = res.replaceAll(MORAL, nf.format(pReport.getMoral()) + "%");
    nf.setMinimumFractionDigits(1);
    nf.setMaximumFractionDigits(1);
    res = res.replaceAll(LUCK_NEG,
            ((pReport.getLuck() < 0) ? "<b>" + nf.format(pReport.getLuck()) + "%</b>" : ""));
    res = res.replaceAll(LUCK_POS,
            ((pReport.getLuck() >= 0) ? "<b>" + nf.format(pReport.getLuck()) + "%</b>" : ""));
    res = res.replaceAll(LUCK_ICON1, "<img src=\""
            + ((pReport.getLuck() <= 0) ? FightReportHTMLToolTipGenerator.class.getResource("/res/rabe.png")
                    : FightReportHTMLToolTipGenerator.class.getResource("/res/rabe_grau.png"))
            + "\"/>");
    res = res.replaceAll(LUCK_ICON2, "<img src=\""
            + ((pReport.getLuck() >= 0) ? FightReportHTMLToolTipGenerator.class.getResource("/res/klee.png")
                    : FightReportHTMLToolTipGenerator.class.getResource("/res/klee_grau.png"))
            + "\"/>");
    res = res.replaceAll(ATTACKER, StringEscapeUtils.escapeHtml(pReport.getAttacker().getName()));
    res = res.replaceAll(SOURCE, StringEscapeUtils.escapeHtml(pReport.getSourceVillage().getFullName()));
    res = res.replaceAll(DEFENDER, StringEscapeUtils.escapeHtml(pReport.getDefender().getName()));
    res = res.replaceAll(TARGET, StringEscapeUtils.escapeHtml(pReport.getTargetVillage().getFullName()));
    res = res.replaceAll(RAM_DAMAGE,
            ((pReport.wasWallDamaged())
                    ? "Wall besch&auml;digt von Level <b>" + pReport.getWallBefore() + "</b> auf Level <b>"
                            + pReport.getWallAfter() + "</b>"
                    : ""));
    res = res.replaceAll(CATA_DAMAGE,
            ((pReport.wasBuildingDamaged()) ? pReport.getAimedBuilding() + " besch&auml;digt von Level <b>"
                    + pReport.getBuildingBefore() + "</b> auf Level <b>" + pReport.getBuildingAfter() + "</b>"
                    : ""));
    res = res.replaceAll(SNOB_INFLUENCE,
            ((pReport.wasSnobAttack())
                    ? "Zustimmung gesunken von <b>" + pReport.getAcceptanceBefore() + "</b> auf <b>"
                            + pReport.getAcceptanceAfter() + "</b>"
                    : ""));

    return res;
}

From source file:com.nextgis.mobile.forms.CompassFragment.java

public static String formatNumber(Object value, int max, int min) {

    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(max);//  w  ww  .j  av  a 2s  . c o m
    f.setMinimumFractionDigits(min);
    f.setGroupingUsed(false);

    try {
        return f.format(value);
    } catch (IllegalArgumentException e) {
        return "err";
    }

}