posted 17 years ago
Math.round is a method which accepts one argument of two different types(double/float).There is no empty parameter method defined for Math.round, Since this method is defined to round up some number, which need to be passed to it.
1) static long round(double a)
Returns the closest long to the argument.
2) static int round(float a)
Returns the closest int to the argument.
In the example, when you are using Math.round(value[x]+0.5) inside the for loop, while iterating through the values array you are just passing values[0] which is -2.3, value[1] which is -1.0 ..values[3] to the Math.round funtion. ( Math.round( -2.3+0.5), Math(-1.0+0.5).....etc
Please try the following to understand the output.
for(int x=0;x<values.length;x++){
long j=Math.round(values[x]+.5);
double k=Math.ceil(values[x]);
System.out.println("j :"+j);
System.out.println("k :"+k);
if(Math.round(values[x]+.5) ==(Math.ceil(values[x]))){
++cnt;
}
}
System.out.println("Same results "+cnt+" times(s)");
output:
j :-2
k :-2.0
j :0
k :-1.0
j :1
k :1.0
j :5
k :4.0
Same results 2 times(s)
( cnt value is updated when j and k values are same, in this case j=-2 and k=-2.0 is same and j=1 and j=1.0 is same)