Round a double to 2 decimal places?

suppose if the value is 200.3456 it should be formatted to 200.34 or if it is 200 then it should be 200.00

the best solution for the solving the problem is here

 

 

class Double1

{

public static void main(String args[])

{

double value = 200.3456;

System.out.printf("Value: %.2f", value);

}

}

 

    output

 

    200.34

 

class Double1

{

public static void main(String args[])

{

double value = 200;

System.out.printf("Value: %.2f", value);

}

}

 

   output

 

   200.00

If you just want to print a double with two digits after the decimal point, use something like this:

double value = 200.3456;

System.out.printf("Value: %.2f", value);

 

If you want to have the result in a String instead of being printed to the console, use String.format() with the same arguments:

 

String result = String.format("%.2f", value);

Or 

use class DecimalFormat:

 

DecimalFormat df = new DecimalFormat("####0.00");

System.out.println("Value: " + df.format(value));