Decimal places when using Println in Java

4

I have the following problem when I print this function

System.out.println("Area do Tetraedro =  " + c1.getArea());

The value of it is very large, eg:

  

Area of Tetraedo = 389.71143170299734

How to leave it with two decimal places? And also, how to put the unit of measurement at the end?

    
asked by anonymous 19.05.2014 / 13:52

2 answers

0

You've already resolved, but you could use the class DecimalFormat of the java.text package. It has the format() method that gets a decimal and format as you wish.

    
01.06.2014 / 03:05
5

You can use printf() instead of println() . Example:

System.out.printf("Area do Tetraedro =  %.2f\n", c1.getArea());

Where %.2f means that the second argument of the method, that is, the first after the String, will be represented as a floating point number limited to showing only two decimal places.

\n plays the role of breaking the line, which made println() and that printf() does not automatically.

    
19.05.2014 / 13:55