I have the following code
double area = 6.60;
System.out.println(area);
This gives the output 6.6
How can I make the output be 6.60?
I have the following code
double area = 6.60;
System.out.println(area);
This gives the output 6.6
How can I make the output be 6.60?
You can use DecimalFormat:
double area = 6.60;
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(area));
Output:
6.60
double x = 6.60;
System.out.printf("%.2f",x);
6.60
The .0 specifies the precision. Number is rounded off according to the precision specified here. (e.g. if you want 2 decimal places you would specify .2)
The f specifies it's a floating point - including doubles (d is for decimal integer)