Example usage for java.text NumberFormat setMaximumFractionDigits

List of usage examples for java.text NumberFormat setMaximumFractionDigits

Introduction

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

Prototype

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:com.bdaum.zoom.gps.internal.GpsUtilities.java

public static void getGeoAreas(IPreferenceStore preferenceStore, Collection<GeoArea> areas) {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
    nf.setMaximumFractionDigits(8);
    nf.setGroupingUsed(false);/*from  ww  w . ja v a2  s. c  om*/
    String nogo = preferenceStore.getString(PreferenceConstants.NOGO);
    if (nogo != null) {
        int i = 0;
        double lat = 0, lon = 0, km = 0;
        String name = null;
        StringTokenizer st = new StringTokenizer(nogo, ";,", true); //$NON-NLS-1$
        while (st.hasMoreTokens()) {
            String s = st.nextToken();
            if (";".equals(s)) { //$NON-NLS-1$
                areas.add(new GeoArea(name, lat, lon, km));
                i = 0;
            } else if (",".equals(s)) //$NON-NLS-1$
                ++i;
            else
                try {
                    switch (i) {
                    case 0:
                        name = s;
                        break;
                    case 1:
                        lat = nf.parse(s).doubleValue();
                        break;
                    case 2:
                        lon = nf.parse(s).doubleValue();
                        break;
                    case 3:
                        km = nf.parse(s).doubleValue();
                        break;
                    }
                } catch (ParseException e) {
                    // do nothing
                }
        }
    }
}

From source file:eu.choreos.vv.chart.YIntervalChart.java

private static JFreeChart createChart(XYDataset dataset, String chartTitle, String xLabel, String yLabel) {

    // create the chart...
    JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, // chart title
            xLabel, // domain axis label
            yLabel, // range axis label
            dataset, // initial series
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips?
            false // URLs?
    );/*from ww  w .  jav  a 2 s.c om*/

    // set chart background
    chart.setBackgroundPaint(Color.white);

    // set a few custom plot features
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(new Color(0xffffe0));
    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);

    // set the plot's axes to display integers
    TickUnitSource ticks = NumberAxis.createIntegerTickUnits();
    NumberAxis domain = (NumberAxis) plot.getDomainAxis();
    domain.setStandardTickUnits(ticks);
    domain.resizeRange(1.1);
    domain.setLowerBound(0.5);

    NumberAxis range = (NumberAxis) plot.getRangeAxis();
    range.setStandardTickUnits(ticks);
    range.setUpperBound(range.getUpperBound() * 1.1);

    // render shapes and lines
    DeviationRenderer renderer = new DeviationRenderer(true, true);
    plot.setRenderer(renderer);
    renderer.setBaseShapesVisible(true);
    renderer.setBaseShapesFilled(true);

    // set the renderer's stroke
    Stroke stroke = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
    renderer.setBaseOutlineStroke(stroke);

    //TODO: render the deviation with the same color as the line
    //        renderer.setBaseFillPaint(new Color(255, 200, 200));
    //        renderer.setUseOutlinePaint(flag);
    //        renderer.setUseFillPaint(flag);

    // label the points
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(2);
    XYItemLabelGenerator generator = new StandardXYItemLabelGenerator(
            StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT, format, format);
    renderer.setBaseItemLabelGenerator(generator);
    renderer.setBaseItemLabelsVisible(true);

    //TODO: fix the visible area to show the deviation

    return chart;
}

From source file:net.luna.common.util.ParseUtils.java

public static String doubleToString(double number, int precision) {
    NumberFormat format = NumberFormat.getInstance();
    format.setMaximumFractionDigits(precision);
    return format.format(number);
}

From source file:com.nridge.core.base.std.Platform.java

public static String bytesToString(long aSizeInBytes) {

    NumberFormat numberFormat = new DecimalFormat();
    numberFormat.setMaximumFractionDigits(2);
    try {/* w  w  w  .j  a va 2s  . c  o  m*/
        if (aSizeInBytes < FORMAT_SIZE_IN_KB) {
            return numberFormat.format(aSizeInBytes) + " byte(s)";
        } else if (aSizeInBytes < FORMAT_SIZE_IN_MB) {
            return numberFormat.format(aSizeInBytes / FORMAT_SIZE_IN_KB) + " KB";
        } else if (aSizeInBytes < FORMAT_SIZE_IN_GB) {
            return numberFormat.format(aSizeInBytes / FORMAT_SIZE_IN_MB) + " MB";
        } else if (aSizeInBytes < FORMAT_SIZE_IN_TB) {
            return numberFormat.format(aSizeInBytes / FORMAT_SIZE_IN_GB) + " GB";
        } else {
            return numberFormat.format(aSizeInBytes / FORMAT_SIZE_IN_TB) + " TB";
        }
    } catch (Exception e) {
        return aSizeInBytes + " byte(s)";
    }
}

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 ww w .  j av  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:is.landsbokasafn.deduplicator.heritrix.DeDuplicator.java

protected static String getPercentage(double portion, double total) {
    NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.ENGLISH);
    percentFormat.setMaximumFractionDigits(1);
    return percentFormat.format(portion / total);
}

From source file:com.mongodb.hadoop.examples.wordcount.split.WordCountSplitTest.java

private final static void test(boolean useShards, boolean useChunks, Boolean slaveok, boolean useQuery)
        throws Exception {
    final Configuration conf = new Configuration();
    MongoConfigUtil.setInputURI(conf, "mongodb://localhost:30000/test.lines");
    conf.setBoolean(MongoConfigUtil.SPLITS_USE_SHARDS, useShards);
    conf.setBoolean(MongoConfigUtil.SPLITS_USE_CHUNKS, useChunks);

    if (useQuery) {
        //NOTE: must do this BEFORE Job is created
        final MongoConfig mongo_conf = new MongoConfig(conf);
        com.mongodb.BasicDBObject query = new com.mongodb.BasicDBObject();
        query.put("num", new com.mongodb.BasicDBObject(Collections.singletonMap("$mod", new int[] { 2, 0 })));
        System.out.println(" --- setting query on num");
        mongo_conf.setQuery(query);//from w  ww. j  a v a  2 s  .  c  o  m
        System.out.println(" --- query is: " + mongo_conf.getQuery());
    }

    String output_table = null;
    if (useChunks) {
        if (useShards)
            output_table = "with_shards_and_chunks";
        else
            output_table = "with_chunks";
    } else {
        if (useShards)
            output_table = "with_shards";
        else
            output_table = "no_splits";
    }
    if (slaveok != null) {
        output_table += "_" + slaveok;
    }
    MongoConfigUtil.setOutputURI(conf, "mongodb://localhost:30000/test." + output_table);
    System.out.println("Conf: " + conf);

    final Job job = new Job(conf, "word count " + output_table);

    job.setJarByClass(WordCountSplitTest.class);

    job.setMapperClass(TokenizerMapper.class);

    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    job.setInputFormatClass(MongoInputFormat.class);
    job.setOutputFormatClass(MongoOutputFormat.class);

    final long start = System.currentTimeMillis();
    System.out.println(" ----------------------- running test " + output_table + " --------------------");
    try {
        boolean result = job.waitForCompletion(true);
        System.out.println("job.waitForCompletion( true ) returned " + result);
    } catch (Exception e) {
        System.out.println("job.waitForCompletion( true ) threw Exception");
        e.printStackTrace();
    }
    final long end = System.currentTimeMillis();
    final float seconds = ((float) (end - start)) / 1000;
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
    nf.setMaximumFractionDigits(3);
    System.out.println("finished run in " + nf.format(seconds) + " seconds");

    com.mongodb.Mongo m = new com.mongodb.Mongo(
            new com.mongodb.MongoURI("mongodb://localhost:30000/?slaveok=true"));
    com.mongodb.DB db = m.getDB("test");
    com.mongodb.DBCollection coll = db.getCollection(output_table);
    com.mongodb.BasicDBObject query = new com.mongodb.BasicDBObject();
    query.put("_id", "the");
    com.mongodb.DBCursor cur = coll.find(query);
    if (!cur.hasNext())
        System.out.println("FAILURE: could not find count of \'the\'");
    else
        System.out.println("'the' count: " + cur.next());

    //        if (! result)
    //           System.exit(  1 );
}

From source file:org.punksearch.web.statistics.FileTypeStatistics.java

public static PieDataset makePieDataset(Map<String, Long> values, long total) {
    long sum = 0;
    for (Long value : values.values()) {
        sum += value;/* w ww  .j ava  2 s  .c  o m*/
    }
    long other = total - sum;

    NumberFormat nfPercent = NumberFormat.getPercentInstance();
    NumberFormat nfNumber = NumberFormat.getNumberInstance();
    nfPercent.setMaximumFractionDigits(2);

    DefaultPieDataset dataset = new DefaultPieDataset();
    for (Map.Entry<String, Long> entry : values.entrySet()) {
        long value = entry.getValue();
        dataset.setValue(entry.getKey() + " " + nfPercent.format(value / (total + 0.0)) + " ("
                + nfNumber.format(value) + ")", value);
    }
    if (other > 0) {
        dataset.setValue(
                "other " + nfPercent.format(other / (total + 0.0)) + " (" + nfNumber.format(other) + ")",
                other);
    }

    return dataset;
}

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

/**
 * Write a number into a string value.//from  w w w. j  a v  a 2 s  .  c o  m
 *
 * @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: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);
    }/*www . jav a 2s .  c  o m*/

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