import java.math.BigDecimal;
public class TestRound {
/**
*
* @param d
* @param decimalPlace
* @return
*/
public static double round(double d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Double.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_DOWN);
return bd.doubleValue();
}
public static void main(String args[]) {
double d = 3.1537;
System.out.println(d + " : " + round(d, 2));
System.out.println(d + " : " + round(d, 3));
}
}
Wednesday, September 19, 2007
Rounding a number in Java
Often we face a problem to round a number to a desired position. This program will round the number to a desired position.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment