-1

i have a method printing times such as: 1.23455 1.26789 1.27853

i want it to be shortened to only two decimal places such as : 1.23 1.26 1.27

      System.out.println("time: "+ time);
  • 2
    possible duplicate of [How to display an output of float data with 2 decimal places in Java?](http://stackoverflow.com/questions/2538787/how-to-display-an-output-of-float-data-with-2-decimal-places-in-java) – Simon Jul 08 '14 at 17:59
  • Here have a look at this [question](http://stackoverflow.com/questions/5051395/java-float-123-129456-to-123-12-without-rounding). It answers your question thoroughly. – Native Jul 08 '14 at 18:01

1 Answers1

3

Many, many, many ways... For example you can use String#format(String format, Object... arg)

System.out.println(String.format("time: %.2f", time));

Or using printf, or DecimalFormat. I let you googlize it ;)

NiziL
  • 5,068
  • 23
  • 33