Rounding double/float till 1 decimal value
1) Math.round(x)
Prints
10.5 rounded is 11
10.499999999999998 rounded is 10
9.5 rounded is 10
9.499999999999998 rounded is 9
8.5 rounded is 9
8.499999999999998 rounded is 8
7.5 rounded is 8
7.499999999999999 rounded is 7
Rounding double/float till 2 Decimal values
Eg:
double kilobytes = 1205.6358;
System.out.println("kilobytes : " + kilobytes);
double newKB = Math.round(kilobytes*100.0)/100.0;
System.out.println("kilobytes (Math.round) : " + newKB);
Output :
=======
1) Math.round(x)
Prints
10.5 rounded is 11
10.499999999999998 rounded is 10
9.5 rounded is 10
9.499999999999998 rounded is 9
8.5 rounded is 9
8.499999999999998 rounded is 8
7.5 rounded is 8
7.499999999999999 rounded is 7
Rounding double/float till 2 Decimal values
Eg:
double kilobytes = 1205.6358;
System.out.println("kilobytes : " + kilobytes);
double newKB = Math.round(kilobytes*100.0)/100.0;
System.out.println("kilobytes (Math.round) : " + newKB);
Output :
=======
kilobytes : 1205.6358
kilobytes (Math.round) : 1205.64