xxxxxxxxxx
double number = 3.7;
int roundedNumber = (int) Math.round(number);
System.out.println(roundedNumber);
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
//Round Doubles in Java to two decimal places.
double dbl = 5211.1246;
System.out.println("Original Double value: "+dbl);
double roundVal = Math.round(dbl*100.0)/100.0;
System.out.println("Updated rounded Double value: "+roundVal);
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
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