Example usage for java.text DecimalFormat setMinimumIntegerDigits

List of usage examples for java.text DecimalFormat setMinimumIntegerDigits

Introduction

In this page you can find the example usage for java.text DecimalFormat setMinimumIntegerDigits.

Prototype

@Override
public void setMinimumIntegerDigits(int newValue) 

Source Link

Document

Sets the minimum number of digits allowed in the integer portion of a number.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();

    format.setMinimumIntegerDigits(4);

    System.out.println(format.format(123456789.12345678));

}

From source file:Main.java

public static DecimalFormat createDecimalFormat(int minInt, int maxInt, int minFract, int maxFract,
        char separator, RoundingMode mode) {

    DecimalFormat format = (DecimalFormat) DecimalFormat.getNumberInstance();
    format.setRoundingMode(mode);/*from ww w . j a va2 s  .  c o  m*/

    format.setMaximumFractionDigits(maxFract);
    format.setMinimumFractionDigits(minFract);
    format.setMaximumIntegerDigits(maxInt);
    format.setMinimumIntegerDigits(minInt);
    DecimalFormatSymbols decimalSymbolComma = new DecimalFormatSymbols();
    decimalSymbolComma.setDecimalSeparator(separator);
    format.setDecimalFormatSymbols(decimalSymbolComma);
    format.setGroupingUsed(false);

    return format;
}

From source file:ai.grakn.graql.internal.util.StringConverter.java

/**
 * @param value a value in the graph/*  w  w w  . j a  va  2s. co  m*/
 * @return the string representation of the value (using quotes if it is already a string)
 */
public static String valueToString(Object value) {
    if (value instanceof String) {
        return quoteString((String) value);
    } else if (value instanceof Double) {
        DecimalFormat df = new DecimalFormat("#", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
        df.setMinimumFractionDigits(1);
        df.setMaximumFractionDigits(12);
        df.setMinimumIntegerDigits(1);
        return df.format(value);
    } else {
        return value.toString();
    }
}

From source file:com.streamreduce.util.MessageUtils.java

public static String roundAndTruncate(double rawValue, int precision) {
    DecimalFormat df = new DecimalFormat();

    df.setMaximumFractionDigits(precision);
    df.setMinimumFractionDigits(precision);
    df.setMinimumIntegerDigits(1);

    return df.format(rawValue);
}

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  ww  w  . j  av a 2  s  . co  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:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

public static String summarizeDetailedOccupancyStats(Map<Id<Vehicle>, double[]> vehicleDistances, String del,
        int maxcap) {
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(2);//  ww  w  . jav a 2 s . c o m
    format.setGroupingUsed(false);

    double[] sum = new double[maxcap + 1];

    for (double[] dist : vehicleDistances.values()) {
        double emptyD = dist[0] - dist[2];
        sum[0] += emptyD;
        for (int i = 3; i < maxcap + 3; i++) {
            sum[i - 2] += dist[i];
        }
    }
    String result = "";
    for (int i = 0; i <= maxcap; i++) {
        result = result + ";" + format.format(sum[i]);
    }

    return result;
}

From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

/**
 * @param vehicleDistances/*from   ww  w.j  a v a 2s. c  o  m*/
 * @param string
 * @return
 */
public static String summarizeVehicles(Map<Id<Vehicle>, double[]> vehicleDistances, String del) {
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(2);
    format.setGroupingUsed(false);

    DescriptiveStatistics driven = new DescriptiveStatistics();
    DescriptiveStatistics revenue = new DescriptiveStatistics();
    DescriptiveStatistics occupied = new DescriptiveStatistics();
    DescriptiveStatistics empty = new DescriptiveStatistics();

    for (double[] dist : vehicleDistances.values()) {
        driven.addValue(dist[0]);
        revenue.addValue(dist[1]);
        occupied.addValue(dist[2]);
        double emptyD = dist[0] - dist[2];
        empty.addValue(emptyD);
    }
    double d_r_d_t = revenue.getSum() / driven.getSum();
    // bw.write("iteration;vehicles;totalDistance;totalEmptyDistance;emptyRatio;totalRevenueDistance;averageDrivenDistance;averageEmptyDistance;averageRevenueDistance");
    String result = vehicleDistances.size() + del + format.format(driven.getSum()) + del
            + format.format(empty.getSum()) + del + format.format(empty.getSum() / driven.getSum()) + del
            + format.format(revenue.getSum()) + del + format.format(driven.getMean()) + del
            + format.format(empty.getMean()) + del + format.format(revenue.getMean()) + del
            + format.format(d_r_d_t);
    return result;
}

From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

/**
 * @param vehicleDistances// w w  w  .j a v  a  2s. c  o m
 * @param iterationFilename
 */
public static void writeVehicleDistances(Map<Id<Vehicle>, double[]> vehicleDistances,
        String iterationFilename) {
    String header = "vehicleId;drivenDistance_m;occupiedDistance_m;emptyDistance_m;revenueDistance_pm";
    BufferedWriter bw = IOUtils.getBufferedWriter(iterationFilename);
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(2);
    format.setGroupingUsed(false);
    try {
        bw.write(header);
        for (Entry<Id<Vehicle>, double[]> e : vehicleDistances.entrySet()) {
            double drivenDistance = e.getValue()[0];
            double revenueDistance = e.getValue()[1];
            double occDistance = e.getValue()[2];
            double emptyDistance = drivenDistance - occDistance;
            bw.newLine();
            bw.write(e.getKey().toString() + ";" + format.format(drivenDistance) + ";"
                    + format.format(occDistance) + ";" + format.format(emptyDistance) + ";"
                    + format.format(revenueDistance));
        }
        bw.flush();
        bw.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

public static String summarizeTrips(List<DynModeTrip> trips, String delimiter) {
    DescriptiveStatistics waitStats = new DescriptiveStatistics();
    DescriptiveStatistics rideStats = new DescriptiveStatistics();
    DescriptiveStatistics distanceStats = new DescriptiveStatistics();
    DescriptiveStatistics directDistanceStats = new DescriptiveStatistics();

    DescriptiveStatistics traveltimes = new DescriptiveStatistics();

    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(2);/*w  w w .  j a v  a  2s.com*/
    format.setGroupingUsed(false);

    for (DynModeTrip trip : trips) {
        if (trip.getToLinkId() == null) {
            continue;
        }
        waitStats.addValue(trip.getWaitTime());
        rideStats.addValue(trip.getInVehicleTravelTime());
        distanceStats.addValue(trip.getTravelDistance());
        directDistanceStats.addValue(trip.getUnsharedDistanceEstimate_m());
        traveltimes.addValue(trip.getInVehicleTravelTime() + trip.getWaitTime());
    }
    String value = format.format(waitStats.getValues().length) + delimiter + format.format(waitStats.getMean())
            + delimiter + format.format(waitStats.getMax()) + delimiter
            + format.format(waitStats.getPercentile(95)) + delimiter
            + format.format(waitStats.getPercentile(75)) + delimiter
            + format.format(waitStats.getPercentile(50)) + delimiter + format.format(rideStats.getMean())
            + delimiter + format.format(distanceStats.getMean()) + delimiter
            + format.format(directDistanceStats.getMean()) + delimiter + format.format(traveltimes.getMean());
    return value;
}

From source file:org.mitre.ccv.mapred.CompleteCompositionVectorUtils.java

/**
 * Writes out the {@link SequenceFile} feature vectors in row major (packed) order. No labels are outputed.
 *
 * @param jobConf//from  w  w  w.  ja va2s.c  o m
 * @param input     top level SequenceFile directory path
 * @param output    path to output the matrix
 * @param digits    the maximum number of fraction digits
 * @throws IOException
 */
public static void featureVectors2RowMajorMatrix(JobConf jobConf, String input, String output, int digits)
        throws IOException {
    JobConf conf = new JobConf(jobConf, CalculateCosineDistanceMatrix.class);

    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(digits);
    //format.setMinimumFractionDigits(fractionDigits);
    format.setGroupingUsed(false);

    final Path inputPath = new Path(input);
    final FileSystem fs = inputPath.getFileSystem(conf);
    final Path qInputPath = fs.makeQualified(inputPath);
    final Path outputPath = new Path(output);
    Path[] paths = FileUtils.ls(conf, qInputPath.toString() + Path.SEPARATOR + "part-*");

    FSDataOutputStream fos = fs.create(outputPath, true); // throws nothing!
    final Writer writer = new OutputStreamWriter(fos);
    final Text key = new Text();
    final SparseVectorWritable value = new SparseVectorWritable();
    for (int idx = 0; idx < paths.length; idx++) {
        SequenceFile.Reader reader = new SequenceFile.Reader(fs, paths[idx], conf);
        boolean hasNext = reader.next(key, value);
        while (hasNext) {

            final SparseVector vector = value.get();
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < vector.getCardinality(); i++) {
                final String s = format.format(vector.get(i)); // format the number
                sb.append(s);
                sb.append(' ');
            }
            writer.write(sb.toString());
            hasNext = reader.next(key, value);
        }
        try {
            writer.flush();
            reader.close();
        } catch (IOException ioe) {
            // closing the SequenceFile.Reader will throw an exception if the file is over some unknown size
            LOG.debug("Probably caused by closing the SequenceFile.Reader. All is well", ioe);
        }
    }
    try {
        writer.close();
        fos.flush();
        fos.close();
    } catch (IOException ioe) {
        LOG.debug("Caused by distributed cache output stream.", ioe);
    }
}