Example usage for java.text DecimalFormat format

List of usage examples for java.text DecimalFormat format

Introduction

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

Prototype

public final String format(double number) 

Source Link

Document

Specialization of format.

Usage

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

/**
 * @param vehicleDistances/*w  w  w  .  jav  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:lirmm.inria.fr.math.TestUtils.java

/**
 * Asserts the null hypothesis for a ChiSquare test.  Fails and dumps arguments and test
 * statistics if the null hypothesis can be rejected with confidence 100 * (1 - alpha)%
 *
 * @param valueLabels labels for the values of the discrete distribution under test
 * @param expected expected counts//from w  w w.  j  a v  a  2  s  . c o  m
 * @param observed observed counts
 * @param alpha significance level of the test
 */
public static void assertChiSquareAccept(String[] valueLabels, double[] expected, long[] observed,
        double alpha) {
    ChiSquareTest chiSquareTest = new ChiSquareTest();

    // Fail if we can reject null hypothesis that distributions are the same
    if (chiSquareTest.chiSquareTest(expected, observed, alpha)) {
        StringBuilder msgBuffer = new StringBuilder();
        DecimalFormat df = new DecimalFormat("#.##");
        msgBuffer.append("Chisquare test failed");
        msgBuffer.append(" p-value = ");
        msgBuffer.append(chiSquareTest.chiSquareTest(expected, observed));
        msgBuffer.append(" chisquare statistic = ");
        msgBuffer.append(chiSquareTest.chiSquare(expected, observed));
        msgBuffer.append(". \n");
        msgBuffer.append("value\texpected\tobserved\n");
        for (int i = 0; i < expected.length; i++) {
            msgBuffer.append(valueLabels[i]);
            msgBuffer.append("\t");
            msgBuffer.append(df.format(expected[i]));
            msgBuffer.append("\t\t");
            msgBuffer.append(observed[i]);
            msgBuffer.append("\n");
        }
        msgBuffer.append("This test can fail randomly due to sampling error with probability ");
        msgBuffer.append(alpha);
        msgBuffer.append(".");
        Assert.fail(msgBuffer.toString());
    }
}

From source file:net.jofm.format.NumberFormat.java

@Override
protected String doFormat(Object value) {
    if (StringUtils.isNotEmpty(format)) {
        DecimalFormat formatter = new DecimalFormat(format);
        return formatter.format(value);
    } else {/* www. ja  va2  s. c o m*/
        return value.toString();
    }
}

From source file:com.swcguild.springmvcwebapp.controller.TipCalcController.java

@RequestMapping(value = "/tipCalc", method = RequestMethod.POST)
public String calculateTip(HttpServletRequest request, Model model) {

    try {//  w  ww  .ja  va 2 s. c o m
        originalAmount = Double.parseDouble(request.getParameter("originalAmount"));
        tipPercentage = Double.parseDouble(request.getParameter("tipPercentage"));

        tipAmount = (tipPercentage * originalAmount) / 100;
        finalAmount = originalAmount + tipAmount;

        DecimalFormat df = new DecimalFormat("#.00");

        model.addAttribute("originalAmount", df.format(originalAmount));
        model.addAttribute("tipAmount", df.format(tipAmount));
        model.addAttribute("tipPercentage", tipPercentage);
        model.addAttribute("finalAmount", df.format(finalAmount));
    } catch (NumberFormatException e) {
    }

    return "tipCalcResponse";
}

From source file:com.wabacus.system.datatype.DoubleType.java

public String value2label(Object value) {
    if (value == null)
        return "";
    if (!(value instanceof Double))
        return String.valueOf(value);
    if (this.numberformat != null && !this.numberformat.trim().equals("")) {
        DecimalFormat df = new DecimalFormat(this.numberformat);
        return df.format((Double) value);
    } else {//  w w w .  ja  v  a 2  s  .c o  m
        return String.valueOf(value);
    }
}

From source file:com.wabacus.system.datatype.FloatType.java

public String value2label(Object value) {
    if (value == null)
        return "";
    if (!(value instanceof Float))
        return String.valueOf(value);
    if (this.numberformat != null && !this.numberformat.trim().equals("")) {
        DecimalFormat df = new DecimalFormat(this.numberformat);
        return df.format((Float) value);
    } else {//from  ww w  . j av a2s  . co m
        return String.valueOf(value);
    }
}

From source file:org.geppetto.simulation.CustomSerializer.java

@Override
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException {

    if (null == value) {
        // write the word 'null' if there's no value available
        jgen.writeNull();/* w  w w  .j a va 2s.  c  om*/
    } else if (value.equals(Double.NaN)) {
        jgen.writeNumber(Double.NaN);
    } else {
        final String pattern = "#.##";
        final DecimalFormat myFormatter = new DecimalFormat(pattern);
        final String output = myFormatter.format(value).replace(",", ".");
        jgen.writeNumber(output);
    }
}

From source file:com.wabacus.system.datatype.IntType.java

public String value2label(Object value) {
    if (value == null)
        return "";
    if (!(value instanceof Integer))
        return String.valueOf(value);
    if (this.numberformat != null && !this.numberformat.trim().equals("")) {
        DecimalFormat df = new DecimalFormat(this.numberformat);
        return df.format((Integer) value);
    } else {//w w w .j  a  va2s  . c o  m
        return String.valueOf(value);
    }
}

From source file:com.wabacus.system.datatype.LongType.java

public String value2label(Object value) {
    if (value == null)
        return "";
    if (!(value instanceof Long))
        return String.valueOf(value);
    if (this.numberformat != null && !this.numberformat.trim().equals("")) {
        DecimalFormat df = new DecimalFormat(this.numberformat);
        return df.format((Long) value);
    } else {/*from  www  .j  av a 2  s . c  o  m*/
        return String.valueOf(value);
    }
}

From source file:com.eu.evaluation.server.eva.EvaluateExcutorTest.java

@Test
public void test() {
    double d = 0.987;
    logger.info("String.format = " + String.format("%.2f", d));
    DecimalFormat df = new DecimalFormat("*.00");
    logger.info("DecimalFormat = " + df.format(d));

    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(2);/*from  w  ww  . j  a v  a  2  s.  co m*/
    logger.info("NumberFormat = " + nf.format(d));

    DecimalFormat formater = new DecimalFormat();
    formater.setMaximumFractionDigits(2);
    formater.setGroupingSize(0);
    formater.setRoundingMode(RoundingMode.FLOOR);
    logger.info("DecimalFormat = " + (formater.format(d)));
}