Page 103 - CA_Blue( J )_Class10
P. 103

6.1.8 The Math.round( ) Method
                 The Math.round( ) method returns the value after rounding it up to the nearest integer number. If the argument is an
                 integer value, then it will return the same value. For example,
                    int i = Math.round(5);              // Output: 5
                 If the argument is a real positive value, then the value after the decimal is considered according to the following:
                 •  If the decimal value is less than 0.5, then the output will be an integer value only.
                    int i = Math.round(5.2);  //Output: 5
                 •  If the decimal value is more than or equal to 0.5, then the output will be the smallest integer value greater than the
                   number.
                    int i = Math.round(5.62); //Output: 6
                 If the argument is a real negative value, then the value after the decimal is considered according to the following:
                 •  If the decimal value is less than or equal to 0.5, then the output will be an integer value only. The sign will be
                   negative.
                    int i = Math.round(-5.5); //Output: -5
                 •  If the decimal value is more than 0.5, then the output will be the largest integer value smaller than the number. The
                   sign will be negative.

                    int i = Math.round(-5.62); //Output: -6
                 6.1.9 The Math.ceil( ) Method
                 The Math.ceil( ) method is used to return the smallest integer value greater than the number that is provided in the
                 argument. It always returns double type value. Following are some examples of using the Math.ceil( ) method:

                    double d = Math.ceil(3.4);          // Output: 4.0
                    double d = Math.ceil(25.8);               // Output: 26.0
                    double d = Math.ceil(-53.49);             // Output: -53.0
                    double d = Math.ceil(-0.8);               // Output: -0.0
                 6.1.10 The Math.floor( ) Method
                 The Math.floor( ) method returns the largest integer value smaller than the number that is provided as an argument.
                 It always returns double type value. Following are some examples of using the Math.floor( ) method:

                    double d = Math.floor(3.4);               // Output: 3.0
                    double d = Math.floor(25.8);              // Output: 25.0
                    double d = Math.floor(-53.49);            // Output: -54.0
                    double d = Math.floor(-0.8);              // Output: -1.0
                 6.1.11 The Math.rint( ) Method
                 The Math.rint( ) method returns the nearest integer value of the fractional number provided as an argument. The
                 value before the decimal point is considered as follows:
                 •  If it is an even value then the method will return the previous integer value.
                 •  If it is an odd value then the method will return the next integer value.
                 For positive fractional numbers:
                    int i = Math.rint(7.8);             // Output: 8.0
                    int i = Math.rint(7.2);             // Output: 7.0
                    int i = Math.rint(7.5);             // Output: 8.0
                    int i = Math.rint(4.5);             // Output: 4.0
                    int i = Math.rint(9.5);             // Output: 10.0
                 For negative fractional numbers:

                    int i = Math.rint(-7.8);            // Output: -8.0
                    int i = Math.rint(-7.2);            // Output: -7.0
                    int i = Math.rint(-7.5);            // Output: -8.0



                                                                                                                       101
                                                                                          Mathematical Library Methods   101
   98   99   100   101   102   103   104   105   106   107   108