xxxxxxxxxx
double notRoundNum = 231.12321
DecimalFormat df = new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.ENGLISH));
// (#.## sets the number of decimal places)
// (Decimals format symbols sets '.' as the symbol for decimals, if it's not defined like that as default)
double roundNum = Double.parseDouble(df.format(notRoundNum)); // = 123.12
xxxxxxxxxx
double roundOff = Math.round(a * 100.0) / 100.0;
// Or
double roundOff = (double) Math.round(a * 100) / 100;
// both have the same output.
xxxxxxxxxx
class round{
public static void main(String args[]){
double a = 123.13698;
double roundOff = Math.round(a*100)/100;
System.out.println(roundOff);
}
}
xxxxxxxxxx
// calling the function round
round(200.3456, 2);
// Include this function in your class
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = BigDecimal.valueOf(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
xxxxxxxxxx
class round{
public static void main(String args[]){
double a = 123.13698;
double roundOff = Math.round(a*100)/100; //put attention to the parenthesis.
System.out.println(roundOff);
}
}
xxxxxxxxxx
double number = 3.7;
int roundedNumber = (int) Math.round(number);
System.out.println(roundedNumber);