rounding a double
Hi all,as you can tell from my question I am new to JAVA. So far I love it.I need to round a double to 2 places but the Math.round method rounds to an integer. Does someone have a good way to round to 2 places? ThanksMessage was edited by: LTLhawk
[289 byte] By [
LTLhawka] at [2007-11-27 2:02:01]

well, I would love to see both, but for this project I just need a string that will be formatted to hold the double to 2 placesMessage was edited by: LTLhawk
> well, I would love to see both, but for this project. I just need a string > that will be formatted to hold the double to 2 placesHave a look at the DecimalFormat class.kind regards,Jos
You can use NumberFormat:
import java.text.*;
public class RoundedExample {
public static void main(String[] args) {
double x = 1.235;
NumberFormat fmt = NumberFormat.getNumberInstance();
fmt.setMinimumFractionDigits(2);
fmt.setMaximumFractionDigits(2);
String s = fmt.format(x);
System.out.println(s);
}
}
There is also double formatting capabilities built into java.util.Formatter
and the format method of java.io.PrintWriter/PrintStream.