Floor returns the largest integer not greater than
Round is the calculation of 4 5 in, when it is greater than its integer
Round method, which means "rounding", the algorithm is Math.floor (x+0.5), the original number is added 0.5 and then rounded down, so the result of Math.Round (11.5) is 12,math.round (-11.5) The result is-11.
Ceil is not less than his smallest integer
See Example
|
Math.floor |
Math.Round |
Math.ceil |
1.4 |
1 |
1 |
2 |
1.5 |
1 |
2 |
2 |
1.6 |
1 |
2 |
2 |
-1.4 |
-2 |
-1 |
-1 |
-1.5 |
-2 |
-1 |
-1 |
-1.6 |
-2 |
-2 |
-1 |
The test procedure is as follows:
[Java]View Plaincopyprint?
- Public class MyTest {
- public static void Main (string[] args) {
- double[] Nums = { 1.4, 1.5, 1.6,-1.4,-1.5,-1.6};
- For (double num:nums) {
- Test (NUM);
- }
- }
- private static void Test (double num) {
- System.out.println ("math.floor (" + num + ") =" + Math.floor (num));
- System.out.println ("math.round (" + num + ") =" + Math.Round (num));
- System.out.println ("math.ceil (" + num + ") =" + Math.ceil (num));
- }
- }
Run results
Math.floor (1.4) =1.0
Math.Round (1.4) =1
Math.ceil (1.4) =2.0
Math.floor (1.5) =1.0
Math.Round (1.5) =2
Math.ceil (1.5) =2.0
Math.floor (1.6) =1.0
Math.Round (1.6) =2
Math.ceil (1.6) =2.0
Math.floor (-1.4) =-2.0
Math.Round (-1.4) =-1
Math.ceil (-1.4) =-1.0
Math.floor (-1.5) =-2.0
Math.Round (-1.5) =-1
Math.ceil (-1.5) =-1.0
Math.floor (-1.6) =-2.0
Math.Round (-1.6) =-2
Math.ceil (-1.6) =-1.0
http://blog.csdn.net/foart/article/details/4295645
1. Using the Math.Round () method:
Divides the number of two int, and the result retains two digits after the decimal point:
int a=1188;
int b=93;
Double C;
C= (Double) (Math.Round (A/b)/100.0);//This keeps 2 bits
Print Result: c=0.12
C=new Double (Math.Round (A/b)/1000.0);//This keeps 3 bits
Print Result: c=0.012
2. Another approach
Import Java.text.DecimalFormat;
DecimalFormat DF2 = new DecimalFormat ("###.00");//This is to keep 2 bits
DecimalFormat DF2 = new DecimalFormat ("###.000");//This is to keep 3 bits
SYSTEM.OUT.PRINTLN (Df2.format (variable of type double));
Ps:
The role of Math.Round ():
Double a=123.55
System.out.println (Math.Round (a));
Printed results: 124
http://blog.csdn.net/evatian/article/details/4398016
Java Division Two ways to keep the two digits after the decimal point summary of Floor,round and ceil of Java Math