Rounding a double to 2 decimal places

Stupid but I don't have my book and I can'tfind "round" examples in the API. If anyone canhelp, it'd be great. Thanx,
[148 byte] By [olerag] at [2007-9-26 1:21:35]
# 1
See the class java.util.DecimalFormat
jsalonen at 2007-6-29 0:57:27 > top of Java-index,Archived Forums,Java Programming...
# 2

Here's a quick and dirty example to do it explicitly:

class Rounder{

public static void main(String[] args){

double d = 18.4567;

double rounded = (int)(100*d+0.5)/100.0;

System.out.println("rounded number = "+rounded);

}

}

If you meant "truncate" instead of round, leave off the 0.5.

bvant-hull at 2007-6-29 0:57:27 > top of Java-index,Archived Forums,Java Programming...
# 3

"java.util.DecimalFormat"... What was I thinking! It's java.text.DecimalFormat:import java.text.*;

...

DecimalFormat format = new DecimalFormat("#.00");

StringBuffer formatted = format.format(Math.PI, new StringBuffer(), new FieldPosition(0));

System.out.println(formatted); // prints out 3.14

jsalonen at 2007-6-29 0:57:27 > top of Java-index,Archived Forums,Java Programming...