Thursday, February 12, 2015

Math Function

We have couple of Math functions defined to make math operations more ease. I have listed couple of them in the below example.

package Example1;

public class MathOperator2 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  float x=-10.4f;
  
  //absolute
  System.out.println(Math.abs(x));
  //max and min value
  System.out.println(Math.max(10,100));
  System.out.println(Math.min(10,100));
  //ceil
  System.out.println((int)Math.ceil(100.34));
  //floor
  System.out.println((int)Math.floor(100.34));
  //round
  System.out.println((int)Math.round(100.34));
  //random number
  System.out.println((int)(Math.random()*10));//10 helps you generate number between 1 to 9
 }

}


output:
10.4
100
10
101
100
100
6

>>>
------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment