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:mt.LengthDistribution.java

public static void GetLengthDistributionArray(ArrayList<File> AllMovies, double[] calibration) {

    ArrayList<Double> maxlist = new ArrayList<Double>();
    for (int i = 0; i < AllMovies.size(); ++i) {

        double maxlength = LengthDistribution.Lengthdistro(AllMovies.get(i));

        if (maxlength != Double.NaN && maxlength > 0)
            maxlist.add(maxlength);/*from   w ww  . jav a 2s.  com*/

    }
    Collections.sort(maxlist);

    int min = 0;
    int max = (int) Math.round(maxlist.get(maxlist.size() - 1)) + 1;
    XYSeries counterseries = new XYSeries("MT length distribution");
    XYSeries Logcounterseries = new XYSeries("MT Log length distribution");
    final ArrayList<Point> points = new ArrayList<Point>();
    for (int length = 0; length < max; ++length) {

        HashMap<Integer, Integer> frameseed = new HashMap<Integer, Integer>();

        int count = 0;
        for (int i = 0; i < AllMovies.size(); ++i) {

            File file = AllMovies.get(i);

            double currentlength = LengthDistribution.Lengthdistro(file);

            ArrayList<FLSobject> currentobject = Tracking.loadMTStat(file);

            if (currentlength > length) {

                for (int index = 0; index < currentobject.size(); ++index) {
                    ArrayList<Integer> seedlist = new ArrayList<Integer>();
                    if (currentobject.get(index).length >= length) {
                        seedlist.add(currentobject.get(index).seedID);
                        if (frameseed.get(currentobject.get(index).Framenumber) != null
                                && frameseed.get(currentobject.get(index).Framenumber) != Double.NaN) {

                            int currentcount = frameseed.get(currentobject.get(index).Framenumber);
                            frameseed.put(currentobject.get(index).Framenumber, seedlist.size() + currentcount);
                        } else if (currentobject.get(index) != null)
                            frameseed.put(currentobject.get(index).Framenumber, seedlist.size());

                    }

                }

            }

        }

        // Get maxima length, count
        int maxvalue = Integer.MIN_VALUE;

        for (int key : frameseed.keySet()) {

            int Count = frameseed.get(key);

            if (Count >= maxvalue)
                maxvalue = Count;
        }

        if (maxvalue != Integer.MIN_VALUE) {
            counterseries.add(length, maxvalue);

            if (maxvalue > 0) {
                Logcounterseries.add((length), Math.log(maxvalue));
                points.add(new Point(new double[] { length, Math.log(maxvalue) }));
            }

        }
    }

    final XYSeriesCollection dataset = new XYSeriesCollection();
    final XYSeriesCollection nofitdataset = new XYSeriesCollection();
    dataset.addSeries(counterseries);
    nofitdataset.addSeries(counterseries);
    final XYSeriesCollection Logdataset = new XYSeriesCollection();
    Logdataset.addSeries(Logcounterseries);

    final JFreeChart chart = ChartFactory.createScatterPlot("MT length distribution", "Number of MT",
            "Length (micrometer)", dataset);

    final JFreeChart nofitchart = ChartFactory.createScatterPlot("MT length distribution", "Number of MT",
            "Length (micrometer)", nofitdataset);

    // Fitting line to log of the length distribution
    interpolation.Polynomial poly = new interpolation.Polynomial(1);
    try {

        poly.fitFunction(points);

    } catch (NotEnoughDataPointsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DisplayPoints.display(nofitchart, new Dimension(800, 500));
    dataset.addSeries(Tracking.drawexpFunction(poly, counterseries.getMinX(), counterseries.getMaxX(), 0.5,
            "Exponential fit"));
    NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
    nf.setMaximumFractionDigits(3);
    TextTitle legendText = new TextTitle("Mean Length" + " : " + nf.format(-1.0 / poly.getCoefficients(1))
            + "  " + "Standard Deviation" + " : " + nf.format(poly.SSE));
    legendText.setPosition(RectangleEdge.RIGHT);

    DisplayPoints.display(chart, new Dimension(800, 500));
    chart.addSubtitle(legendText);

    final JFreeChart logchart = ChartFactory.createScatterPlot("MT Log length distribution", "Number of MT",
            "Length (micrometer)", Logdataset);
    //     DisplayPoints.display(logchart, new Dimension(800, 500));
    for (int i = 1; i >= 0; --i)
        System.out.println(poly.getCoefficients(i) + "  " + "x" + " X to the power of " + i);

    //  Logdataset.addSeries(Tracking.drawFunction(poly, counterseries.getMinX(), counterseries.getMaxX(), 0.5, "Straight line fit"));

    WriteLengthdistroFile(AllMovies, counterseries, 0);

}

From source file:mt.LengthDistribution.java

public static void GetLengthDistributionArrayatTime(ArrayList<File> AllMovies, double[] calibration,
        final int framenumber) {

    ArrayList<Double> maxlist = new ArrayList<Double>();
    for (int i = 0; i < AllMovies.size(); ++i) {

        ArrayList<Pair<Integer, Double>> lengthlist = LengthDistribution.LengthdistroatTime(AllMovies.get(i),
                framenumber);/* w  w  w.  ja v a2 s.  c om*/

        for (int index = 0; index < lengthlist.size(); ++index) {
            if (lengthlist.get(index).getB() != Double.NaN && lengthlist.get(index).getB() > 0)
                maxlist.add(lengthlist.get(index).getB());

        }
    }
    Collections.sort(maxlist);

    int min = 0;
    int max = 0;
    if (maxlist.size() > 0)
        max = (int) Math.round(maxlist.get(maxlist.size() - 1)) + 1;
    XYSeries counterseries = new XYSeries("MT length distribution");
    XYSeries Logcounterseries = new XYSeries("MT Log length distribution");
    final ArrayList<Point> points = new ArrayList<Point>();
    for (int length = 0; length < max; ++length) {

        HashMap<Integer, Integer> frameseed = new HashMap<Integer, Integer>();

        int count = 0;
        for (int i = 0; i < AllMovies.size(); ++i) {

            File file = AllMovies.get(i);

            ArrayList<FLSobject> currentobject = Tracking.loadMTStat(file);

            if (currentobject != null)
                for (int index = 0; index < currentobject.size(); ++index) {
                    ArrayList<Integer> seedlist = new ArrayList<Integer>();
                    if (currentobject.get(index).length >= length
                            && currentobject.get(index).Framenumber == framenumber) {
                        seedlist.add(currentobject.get(index).seedID);
                        if (frameseed.get(currentobject.get(index).Framenumber) != null
                                && frameseed.get(currentobject.get(index).Framenumber) != Double.NaN) {

                            int currentcount = frameseed.get(currentobject.get(index).Framenumber);
                            frameseed.put(currentobject.get(index).Framenumber, seedlist.size() + currentcount);
                        } else if (currentobject.get(index) != null)
                            frameseed.put(currentobject.get(index).Framenumber, seedlist.size());

                    }

                }

        }

        // Get maxima length, count
        int maxvalue = Integer.MIN_VALUE;

        for (int key : frameseed.keySet()) {

            int Count = frameseed.get(key);

            if (Count >= maxvalue)
                maxvalue = Count;
        }

        if (maxvalue != Integer.MIN_VALUE) {
            counterseries.add(length, maxvalue);

            if (maxvalue > 0) {

                System.out.println("Max " + maxvalue);
                Logcounterseries.add((length), Math.log(maxvalue));
                points.add(new Point(new double[] { length, Math.log(maxvalue) }));
            }

        }
    }

    final XYSeriesCollection dataset = new XYSeriesCollection();
    final XYSeriesCollection nofitdataset = new XYSeriesCollection();
    dataset.addSeries(counterseries);
    nofitdataset.addSeries(counterseries);
    final XYSeriesCollection Logdataset = new XYSeriesCollection();
    Logdataset.addSeries(Logcounterseries);

    final JFreeChart chart = ChartFactory.createScatterPlot("MT length distribution", "Number of MT",
            "Length (micrometer)", dataset);
    final JFreeChart nofitchart = ChartFactory.createScatterPlot("MT length distribution", "Number of MT",
            "Length (micrometer)", nofitdataset);
    // Fitting line to log of the length distribution
    interpolation.Polynomial poly = new interpolation.Polynomial(1);
    try {

        poly.fitFunction(points);

    } catch (NotEnoughDataPointsException e) {

    }
    DisplayPoints.display(nofitchart, new Dimension(800, 500));
    dataset.addSeries(Tracking.drawexpFunction(poly, counterseries.getMinX(), counterseries.getMaxX(), 0.5,
            "Exponential fit"));
    NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
    nf.setMaximumFractionDigits(3);
    TextTitle legendText = new TextTitle("Mean Length" + " : " + nf.format(-1.0 / poly.getCoefficients(1))
            + "  " + "Standard Deviation" + " : " + nf.format(poly.SSE));
    legendText.setPosition(RectangleEdge.RIGHT);

    DisplayPoints.display(chart, new Dimension(800, 500));
    chart.addSubtitle(legendText);

    System.out.println("Series count" + dataset.getSeriesCount());
    final JFreeChart logchart = ChartFactory.createScatterPlot("MT Log length distribution",
            "Length (micrometer)", "Number of MT", Logdataset);
    //     DisplayPoints.display(logchart, new Dimension(800, 500));
    for (int i = 1; i >= 0; --i)
        System.out.println(poly.getCoefficients(i) + "  " + "x" + " X to the power of " + i);

    //  Logdataset.addSeries(Tracking.drawFunction(poly, counterseries.getMinX(), counterseries.getMaxX(), 0.5, "Straight line fit"));
    WriteLengthdistroFile(AllMovies, counterseries, framenumber);
}

From source file:org.projectforge.common.NumberHelper.java

public static NumberFormat getNumberFraction2Format(final Locale locale) {
    final NumberFormat format = NumberFormat.getNumberInstance(locale);
    format.setMaximumFractionDigits(2);
    format.setMinimumFractionDigits(2);//from   w  w  w  .j  av a 2s . c o  m
    return format;
}

From source file:org.projectforge.common.NumberHelper.java

public static NumberFormat getNumberFractionFormat(final Locale locale, final int fractionDigits) {
    final NumberFormat format = NumberFormat.getNumberInstance(locale);
    format.setMaximumFractionDigits(fractionDigits);
    format.setMinimumFractionDigits(fractionDigits);
    return format;
}

From source file:password.pwm.util.Helper.java

public static String formatDiskSize(final long diskSize) {
    final float COUNT = 1000;
    if (diskSize < 1) {
        return "n/a";
    }/*from ww w  .  ja  v  a 2s  .  co m*/

    if (diskSize == 0) {
        return "0";
    }

    final NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);

    if (diskSize > COUNT * COUNT * COUNT) {
        final StringBuilder sb = new StringBuilder();
        sb.append(nf.format(diskSize / COUNT / COUNT / COUNT));
        sb.append(" GB");
        return sb.toString();
    }

    if (diskSize > COUNT * COUNT) {
        final StringBuilder sb = new StringBuilder();
        sb.append(nf.format(diskSize / COUNT / COUNT));
        sb.append(" MB");
        return sb.toString();
    }

    return NumberFormat.getInstance().format(diskSize) + " bytes";
}

From source file:com.github.rutvijkumar.twittfuse.Util.java

public static CharSequence formatCount(long count, boolean isUserCount) {
    // TODO Auto-generated method stub
    if (count < 1000) {
        return String.valueOf(count);
    } else {/*from   w  ww . ja v a  2 s.  com*/
        String result = null;

        NumberFormat df = DecimalFormat.getInstance();
        df.setRoundingMode(RoundingMode.DOWN);

        double countWithK = count / 1000.0;
        if (isUserCount) {
            df.setMinimumFractionDigits(0);
            df.setMaximumFractionDigits(0);
        } else {
            df.setMinimumFractionDigits(1);
            df.setMaximumFractionDigits(1);
        }
        result = df.format(countWithK);
        result = result + "K";
        return result;
    }

}

From source file:com.asalfo.wiulgi.util.Utils.java

/**
 * Calculate distance between two LatLng points and format it nicely for
 * display. As this is a sample, it only statically supports metric units.
 * A production app should check locale and support the correct units.
 *//* w  ww  . j  av a 2s  .  c  om*/
@Nullable
public static String formatDistanceBetween(@Nullable LatLng point1, @Nullable LatLng point2) {
    if (point1 == null || point2 == null) {
        return null;
    }

    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    double distance = Math.round(SphericalUtil.computeDistanceBetween(point1, point2));

    // Adjust to KM if M goes over 1000 (see javadoc of method for note
    // on only supporting metric)
    if (distance >= 1000) {
        numberFormat.setMaximumFractionDigits(1);
        return numberFormat.format(distance / 1000) + DISTANCE_KM_POSTFIX;
    }
    return numberFormat.format(distance) + DISTANCE_M_POSTFIX;
}

From source file:org.squale.squaleweb.util.SqualeWebActionUtils.java

/**
 * Factorisation de code, aucun test n'est fait
 * /*from  w ww .j ava  2s.c  o  m*/
 * @param pFloat le float  formater
 * @return la chaine associe au float
 */
private static String formatFloat(float pFloat) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(1);
    nf.setMinimumFractionDigits(1);
    String result = nf.format(pFloat);
    return result;
}

From source file:us.fatehi.pointlocation6709.format.PointLocationFormatter.java

private static NumberFormat getNumberFormat(final int integerDigits) {
    final NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setMinimumIntegerDigits(integerDigits);
    numberFormat.setMinimumFractionDigits(5);
    numberFormat.setMaximumFractionDigits(5);
    numberFormat.setGroupingUsed(false);
    return numberFormat;
}

From source file:at.alladin.rmbt.qos.testscript.TestScriptInterpreter.java

/**
 * //from   w  w  w. j  av a  2s  .  c  o  m
 * @param args
 * @return
 * @throws ScriptException
 */
private static Object parse(String[] args, Hstore hstore, Object object, ResultOptions options)
        throws ScriptException {

    if (object == null) {
        throw new ScriptException(ScriptException.ERROR_RESULT_IS_NULL + " PARSE");
    }

    HstoreParser<?> parser = hstore.getParser(object.getClass());

    if (args.length < 1) {
        throw new ScriptException(ScriptException.ERROR_INVALID_ARGUMENT_COUNT + " PARSE: " + args.length);
    }
    if (parser == null) {
        throw new ScriptException(ScriptException.ERROR_PARSER_IS_NULL + " PARSE");
    }

    try {
        Pattern p = PATTERN_ARRAY;
        Matcher m = p.matcher(args[0]);

        if (m.find()) {
            String param = m.group(1);
            int index = Integer.valueOf(m.group(2));
            Object array = parser.getValue(param, object);

            Object indexedObject = null;
            if (array != null) {
                if (List.class.isAssignableFrom(array.getClass())) {
                    indexedObject = ((List<?>) array).get(index);
                } else if (Collection.class.isAssignableFrom(array.getClass())) {
                    Iterator<?> iterator = ((Collection<?>) array).iterator();
                    int counter = 0;
                    while (iterator.hasNext()) {
                        Object o = iterator.next();
                        if ((counter++) == index) {
                            indexedObject = o;
                            break;
                        }
                    }
                }

                if (args.length > 1) {
                    String[] nextArgs = new String[args.length - 1];
                    nextArgs = Arrays.copyOfRange(args, 1, args.length);
                    return parse(nextArgs, hstore, indexedObject, options);
                } else {
                    return indexedObject;
                }
            }
        } else {
            Object value = parser.getValue(args[0], object);
            if (args.length > 1) {
                try {
                    long divisor = Long.parseLong(args[1]);
                    int precision = 2;
                    boolean groupingUsed = false;
                    if (args.length > 2) {
                        precision = Integer.parseInt(args[2]);
                    }
                    if (args.length > 3) {
                        groupingUsed = ("t".equals(args[3].toLowerCase())
                                || "true".equals(args[3].toLowerCase()));
                    }
                    NumberFormat format = (options != null ? DecimalFormat.getInstance(options.getLocale())
                            : DecimalFormat.getInstance());
                    format.setMaximumFractionDigits(precision);
                    format.setGroupingUsed(groupingUsed);
                    format.setRoundingMode(RoundingMode.HALF_UP);
                    //System.out.println("Converting number: " + args[0] + "=" + String.valueOf(value));
                    BigDecimal number = new BigDecimal(String.valueOf(value));
                    return format.format(number.divide(new BigDecimal(divisor)));
                } catch (Exception e) {
                    //can not return parsed element
                }
            }
            //System.out.println("PARAM object: " + args[0] + " -> " + value + " of " + object.toString());
            return value;
        }
    } catch (HstoreParseException e) {
        throw new ScriptException(ScriptException.ERROR_UNKNOWN + " PARSE: " + e.getMessage());
    } catch (Throwable t) {
        throw new ScriptException(ScriptException.ERROR_UNKNOWN + " PARSE: " + t.getMessage());
    }

    return null;
}