Possible Duplicate:
Round to 2 decimal places
Suppose in a variable i have a decimal number like 3.1426384473 but i want 3.14. now how i can format decimal numbers up to two decimals points like in above example.
Possible Duplicate:
Round to 2 decimal places
Suppose in a variable i have a decimal number like 3.1426384473 but i want 3.14. now how i can format decimal numbers up to two decimals points like in above example.
try this....
DecimalFormat df = new DecimalFormat("#.##");
df.format(3.1426384473);
Or if u just wanna print then u can use this also...
System.out.printf("%.2f",d); //d is your number
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}