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:de.tor.tribes.util.bb.DefStatsFormatter.java

private String[] getStatSpecificReplacements(Stats pStats) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(0);/*from  w  w  w.  ja  v  a  2s.c  om*/
    String tribe = pStats.getParent().getTribe().toBBCode();
    String killsBefore = nf.format(pStats.getBashDefStart());
    String killsAfter = nf.format(pStats.getBashDefEnd());
    String killsDiff = nf.format(pStats.getBashDefDiff());

    if (pStats.getBashDefDiff() > 0) {
        killsDiff = "[color=green]" + killsDiff + "[/color]";
    } else {
        killsDiff = "[color=red]" + killsDiff + "[/color]";
    }

    long pBefore = pStats.getBashDefStart();
    double perc = 0;
    if (pBefore > 0) {
        perc = (double) 100 * (double) pStats.getBashDefDiff() / (double) pBefore;
    }

    String percentDiff = ((perc >= 0) ? "+" : "") + nf.format(perc) + "%";

    if (perc > 0) {
        percentDiff = "[color=green]" + percentDiff + "[/color]";
    } else {
        percentDiff = "[color=red]" + percentDiff + "[/color]";
    }

    return new String[] { tribe, killsBefore, killsAfter, killsDiff, percentDiff };
}

From source file:de.tor.tribes.util.bb.KillStatsFormatter.java

private String[] getStatSpecificReplacements(Stats pStats) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(0);//  ww  w . java  2 s  .c o  m
    String tribe = pStats.getParent().getTribe().toBBCode();
    String killsBefore = nf.format(pStats.getBashOffStart());
    String killsAfter = nf.format(pStats.getBashOffEnd());
    String killsDiff = nf.format(pStats.getBashOffDiff());

    if (pStats.getBashOffDiff() > 0) {
        killsDiff = "[color=green]" + killsDiff + "[/color]";
    } else {
        killsDiff = "[color=red]" + killsDiff + "[/color]";
    }

    long pBefore = pStats.getBashOffStart();
    double perc = 0;
    if (pBefore > 0) {
        perc = (double) 100 * (double) pStats.getBashOffDiff() / (double) pBefore;
    }

    String percentDiff = ((perc >= 0) ? "+" : "") + nf.format(perc) + "%";

    if (perc > 0) {
        percentDiff = "[color=green]" + percentDiff + "[/color]";
    } else {
        percentDiff = "[color=red]" + percentDiff + "[/color]";
    }

    return new String[] { tribe, killsBefore, killsAfter, killsDiff, percentDiff };
}

From source file:de.tor.tribes.util.bb.PointStatsFormatter.java

private String[] getStatSpecificReplacements(Stats pStats) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(0);//from w  w  w  .  j a  v a2s . c  om
    String tribe = pStats.getParent().getTribe().toBBCode();
    String pointsBefore = nf.format(pStats.getPointStart());
    String pointsAfter = nf.format(pStats.getPointEnd());
    String pointsDiff = nf.format(pStats.getPointDiff());

    if (pStats.getPointDiff() > 0) {
        pointsDiff = "[color=green]" + pointsDiff + "[/color]";
    } else {
        pointsDiff = "[color=red]" + pointsDiff + "[/color]";
    }

    double perc = pStats.getExpansion();
    String percentDiff = ((perc >= 0) ? "+" : "") + nf.format(perc) + "%";

    if (perc > 0) {
        percentDiff = "[color=green]" + percentDiff + "[/color]";
    } else {
        percentDiff = "[color=red]" + percentDiff + "[/color]";
    }

    String killsPerPoints = nf.format(pStats.getKillPerPoint());

    return new String[] { tribe, pointsBefore, pointsAfter, pointsDiff, percentDiff, killsPerPoints };
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.LocationObj.java

@Override
public void render(Context context, ViewGroup frame, Obj obj, boolean allowInteractions) {
    JSONObject content = obj.getJson();//from   w  w  w  .ja  v a  2 s . c o m
    TextView valueTV = new TextView(context);
    NumberFormat df = DecimalFormat.getNumberInstance();
    df.setMaximumFractionDigits(5);
    df.setMinimumFractionDigits(5);

    String msg = "I'm at " + df.format(content.optDouble(COORD_LAT)) + ", "
            + df.format(content.optDouble(COORD_LONG));

    valueTV.setText(msg);
    valueTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    valueTV.setGravity(Gravity.TOP | Gravity.LEFT);
    frame.addView(valueTV);
}

From source file:de.tor.tribes.util.bb.BasicFormatter.java

public NumberFormat getNumberFormatter(int pDigits) {
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(0);/*from w  ww. j a  v  a 2 s . co m*/
    f.setMinimumFractionDigits(0);
    if (pDigits < 10) {
        f.setMaximumIntegerDigits(1);
    } else if (pDigits < 100) {
        f.setMaximumIntegerDigits(2);
    } else if (pDigits < 1000) {
        f.setMaximumIntegerDigits(3);
    } else if (pDigits < 10000) {
        f.setMaximumIntegerDigits(4);
    } else {
        f.setMaximumIntegerDigits(5);
    }
    return f;
}

From source file:org.polymap.kaps.importer.test.NumberFormatterTest.java

public void testThousands() {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
    nf.setMaximumIntegerDigits(10);// w w  w .  j  a  va 2  s. c o  m
    nf.setMaximumFractionDigits(10);
    nf.setMinimumIntegerDigits(10);
    nf.setMinimumFractionDigits(10);

    System.out.println(nf.format(12345));
}

From source file:com.bdb.weather.display.summary.TemperatureBinSummaryPlot.java

public TemperatureBinSummaryPlot() {
    plot = new CombinedDomainCategoryPlot();
    JFreeChart chart = new JFreeChart(plot);
    setMaxHeight(10000);//from ww w .  j  a  va 2s . c  om
    setMaxWidth(10000);

    NumberAxis countAxis = new NumberAxis("Day Count");
    countAxis.setUpperMargin(.2);
    countPlot.setRangeAxis(countAxis);
    countPlot.setDataset(countDataset);

    BarRenderer r = new BarRenderer();
    r.setBaseItemLabelsVisible(true);
    r.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    r.setSeriesPaint(0, Color.BLUE);
    r.setBasePaint(Color.BLUE);
    countPlot.setRenderer(r);

    NumberAxis durationAxis = new NumberAxis("Duration (Hours)");
    durationAxis.setUpperMargin(.2);
    durationPlot.setRangeAxis(durationAxis);
    durationPlot.setDataset(durationDataset);
    r = new BarRenderer();
    r.setBaseItemLabelsVisible(true);
    NumberFormat format = DecimalFormat.getNumberInstance();
    format.setMaximumFractionDigits(1);
    format.setMinimumFractionDigits(1);
    r.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(
            StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, format));
    r.setSeriesPaint(0, Color.RED);
    r.setBasePaint(Color.RED);
    durationPlot.setRenderer(r);

    plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
    plot.add(countPlot);
    plot.add(durationPlot);

    chartViewer = new ChartViewer(chart);
    this.setCenter(chartViewer);
}

From source file:org.asqatasun.webapp.command.factory.ChangeTestWeightCommandFactory.java

/**
 * //from ww  w  . j av  a 2s .c  o  m
 * @param user
 * @param locale
 * @param testList
 * @param referentialKey
 * @return 
 *      an initialised instance of ChangeTestWeightCommand
 */
public ChangeTestWeightCommand getChangeTestWeightCommand(User user, Locale locale, Collection<Test> testList,
        String referentialKey) {
    Map<String, String> userTestWeight = new HashMap<String, String>();
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    nf.setMinimumFractionDigits(1);
    nf.setMaximumFractionDigits(1);
    nf.setMaximumIntegerDigits(1);
    nf.setMinimumIntegerDigits(1);
    for (OptionElement oe : optionElementDataService.getOptionElementFromUserAndFamilyCode(user,
            referentialKey + "_" + optionFamilyCodeStr)) {
        userTestWeight.put(oe.getOption().getCode(), nf.format(Double.valueOf(oe.getValue())));
    }
    for (Test test : testList) {
        if (!userTestWeight.containsKey(test.getCode())) {
            userTestWeight.put(test.getCode(), "");
        }
    }

    ChangeTestWeightCommand changeTestWeightCommand = new ChangeTestWeightCommand();
    changeTestWeightCommand.setTestWeightMap(userTestWeight);
    return changeTestWeightCommand;
}

From source file:org.kalypso.ogc.sensor.timeseries.TimeseriesUtils.java

/**
 * Returns the adequate NumberFormat for the given format-string. It currently only supports formats of the form %X.Yf
 * where actually only the Y is used to build a NumberFormat with Y minimum/maximum-fraction-digits.
 * <p>//from  w  ww. ja v a2 s .c o  m
 * The plan is, once we'll be using JDK 5.0, we'll try to replace this with the built-in functionality provided with
 * formated printing.
 * <p>
 * TODO once on JDK 5.0 use formated printing if possible. Note that some refactoring might need to be done since we
 * currently work with NumberFormats.
 */
public static synchronized NumberFormat getNumberFormat(final String format) {
    final NumberFormat nf = FORMAT_MAP.get(format);
    if (nf != null)
        return nf;

    if ("%d".equals(format)) //$NON-NLS-1$
    {
        final NumberFormat wf = NumberFormat.getIntegerInstance();
        wf.setGroupingUsed(false);
        FORMAT_MAP.put(format, wf);
        return wf;
    }

    // parse the format spec and only take the min-fraction-digit part
    final String regex = "%([0-9]*)\\.?([0-9]*)f"; //$NON-NLS-1$
    final Pattern pattern = Pattern.compile(regex);
    final Matcher matcher = pattern.matcher(format);
    if (matcher.matches()) {
        final String minfd = matcher.group(2);

        final NumberFormat wf = NumberFormat.getInstance();
        final int intValue = Integer.valueOf(minfd).intValue();
        wf.setMinimumFractionDigits(intValue);
        wf.setMaximumFractionDigits(intValue);
        FORMAT_MAP.put(format, wf);

        return wf;
    }

    return getDefaultFormat();
}

From source file:de.tor.tribes.util.bb.WinnerLoserStatsFormatter.java

private String[] getStatSpecificReplacements(List<Stats> pStats, boolean pExtended) {

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(0);//from   w w  w  .j  a  v a 2 s  .  c o  m
    //point titles
    Collections.sort(pStats, Stats.POINTS_COMPARATOR);
    String winnerByPoints = pStats.get(0).getParent().getTribe().toBBCode();
    winnerByPoints += " (" + formatValue(pStats.get(0).getPointDiff(), nf) + " Punkte)";
    long pointsLost = pStats.get(pStats.size() - 1).getPointDiff();
    String loserByPoints = "";
    if (pointsLost < 0) {
        loserByPoints = pStats.get(pStats.size() - 1).getParent().getTribe().toBBCode();
        loserByPoints += " (" + formatValue(pStats.get(pStats.size() - 1).getPointDiff(), nf) + " Punkte)";
    } else {
        loserByPoints = "-Keine Punkteverluste vorhanden-";
    }
    //conquer titles
    Collections.sort(pStats, Stats.VILLAGE_COMPARATOR);
    String winnerByConquers = pStats.get(0).getParent().getTribe().toBBCode();
    winnerByConquers += " (" + formatValue(pStats.get(0).getVillageDiff(), nf) + " Drfer)";
    long conquerLost = pStats.get(pStats.size() - 1).getVillageDiff();
    String loserByConquers = "";
    if (conquerLost < 0) {
        pStats.get(pStats.size() - 1).getParent().getTribe().toBBCode();
        loserByConquers += " (" + formatValue(pStats.get(pStats.size() - 1).getVillageDiff(), nf) + " Drfer)";
    } else {
        loserByConquers = "-Keine Dorfverluste vorhanden-";
    }
    //off bash titles
    Collections.sort(pStats, Stats.BASH_OFF_COMPARATOR);
    String winnerByOffense = pStats.get(0).getParent().getTribe().toBBCode();
    winnerByOffense += " (" + formatValue(pStats.get(0).getBashOffDiff(), nf) + " besiegte Gegner)";
    String loserByOffense = pStats.get(pStats.size() - 1).getParent().getTribe().toBBCode();
    loserByOffense += " (" + formatValue(pStats.get(pStats.size() - 1).getBashOffDiff(), nf)
            + " besiegte Gegner)";
    //def bash titles
    Collections.sort(pStats, Stats.BASH_DEF_COMPARATOR);
    String winnerByDefense = pStats.get(0).getParent().getTribe().toBBCode();
    winnerByDefense += " (" + formatValue(pStats.get(0).getBashDefDiff(), nf) + " besiegte Gegner)";
    String loserByDefense = pStats.get(pStats.size() - 1).getParent().getTribe().toBBCode();
    loserByDefense += " (" + formatValue(pStats.get(pStats.size() - 1).getBashDefDiff(), nf)
            + " besiegte Gegner)";
    //kpp titles
    Collections.sort(pStats, Stats.KILLS_PER_POINT_COMPARATOR);
    String winnerByKillsPerPoint = pStats.get(0).getParent().getTribe().toBBCode();
    winnerByKillsPerPoint += " (" + formatValue(pStats.get(0).getKillPerPoint(), nf) + " Kills pro Punkt)";
    //expansion title
    Collections.sort(pStats, Stats.EXPANSION_COMPARATOR);
    String winnerByExpansion = pStats.get(0).getParent().getTribe().toBBCode();
    winnerByExpansion += " (" + formatValue(pStats.get(0).getExpansion(), nf) + "% Punktezuwachs)";
    double expansionLost = pStats.get(pStats.size() - 1).getExpansion();
    String loserByExpansion = "";
    if (expansionLost < 0) {
        loserByExpansion = " (" + formatValue(pStats.get(pStats.size() - 1).getExpansion(), nf)
                + "% Punktezuwachs)";
    } else {
        loserByExpansion = "-Keine negatives Wachstum vorhanden-";
    }

    return new String[] { winnerByPoints, winnerByExpansion, winnerByConquers, winnerByOffense, winnerByDefense,
            winnerByKillsPerPoint, loserByPoints, loserByExpansion, loserByConquers, loserByOffense,
            loserByDefense };
}