1double roundOff = Math.round(a * 100.0) / 100.0;
2// Or
3double roundOff = (double) Math.round(a * 100) / 100;
4// both have the same output.
1class round{
2 public static void main(String args[]){
3
4 double a = 123.13698;
5 double roundOff = Math.round(a*100)/100;
6
7 System.out.println(roundOff);
8}
9}
10
1import java.math.RoundingMode;
2import java.text.DecimalFormat;
3
4public class DecimalExample {
5 private static DecimalFormat df = new DecimalFormat("0.00");
6 public static void main(String[] args) {
7 double input = 1205.6358;
8
9 System.out.println("salary : " + input);
10
11 // DecimalFormat, default is RoundingMode.HALF_EVEN
12 System.out.println("salary : " + df.format(input)); //1205.64
13
14 df.setRoundingMode(RoundingMode.DOWN);
15 System.out.println("salary : " + df.format(input)); //1205.63
16
17 df.setRoundingMode(RoundingMode.UP);
18 System.out.println("salary : " + df.format(input)); //1205.64
19 }
20}
1class round{
2 public static void main(String args[]){
3
4 double a = 123.13698;
5 double roundOff = Math.round(a*100)/100;
6
7 System.out.println(roundOff);
8 }
9}
10