Page 188 - Cs_withBlue_J_C11_Flipbook
P. 188

If the decimal value is less than 0.5, then the output will be integer value only else will return the smallest integer
                 value greater than the number. If the decimal value is less than equal to 0.5, then the output will be integer value
                 only else will the largest integer value smaller than the number.
                 Example 1: int i = Math.round(6.35)              Output: 6
                 Example 2: int i = Math.round(-26.87)            Output: -27
              9.   Math.ceil()
                 This function rounds a floating-point number up to the next largest integer value. In other words, it finds the
                 smallest integer value which is greater than or equal to the argument passed to it. It uses a double data type.
                 Example 1: double d = Math.ceil(4.1);            Output: 4.0
                 Example 2: double d = Math.ceil(-58.9);          Output: -58.0

              10. Math.floor()
                 This function rounds a floating-point number down to its nearest integer value. In other words, it finds the largest
                 integer value which is less than or equal to the argument passed to it. It uses a double data type.

                 Example 1: double d = Math.floor(-3.3);          Output: -4.0
                 Example 2: double d = Math.floor(25.8);          Output: 25.0
              11. Math.rint()
                 This function rounds the floating-point argument to its nearest integer in floating-point format.
                 For positive fractional numbers:

                 Example 1: int i = Math.rint(7.8);               Output: 8.0
                 Example 2: int i = Math.rint(7.2);               Output: 7.0
                 Example 3: int i = Math.rint(7.5);               Output: 8.0
                 Example 4: int i = Math.rint(4.5);               Output: 4.0
                 Example 5: int i = Math.rint(9.5);               Output: 10.0
                 [Explanation: In case of 0.5, if it is an even integer value it will return the previous integer value and if it is an odd
                 integer value it will return the next integer value. See examples 3, 4 and 5.]
                 For negative fractional numbers:
                 Example 1: int i = Math.rint(-7.8);              Output: -8.0
                 Example 2: int i = Math.rint(-7.2);              Output: -7.0
                 Example 3: int i = Math.rint(-7.5);              Output: -8.0

              12.  Math.exp()
                  This function returns the exponential value of the passed argument. It uses a double data type.
                 Example 1: double d = Math.exp(2.5);             Output: 12.182493960703473

              13.  Math.random()
                  This function returns a random real number between 0 and 1.
                  Example 1: Math.random();                       Output: 0.001110128
                  Some special calculations using Math.random()
                  1. Using this function, we can generate a random number between 1 and x

                   Say, x=3;
                    int a = (int)(Math.random() * x) + 1;
                    [It will return any integer number from 1 to 3]




                186186  Touchpad Computer Science-XI
   183   184   185   186   187   188   189   190   191   192   193