Page 254 - Web_Application_v2.0_C12_Fb
P. 254

Method Name                  Description          Example

                 min()                   Returns the smallest number   document.write(Math.min(-15,
                                                                       -10));
                                                                       document.write(“<br>”+Math.min
                                                                       (155, 132, 123, 13));

                                                                       Output:
                                                                       -15
                                                                       13

                 ceil()                  Rounds a number up to  document.write(Math.ceil(4.2));
                                         the nearest integer.          Output:
                                                                       5

                 floor()                 Rounds a  number  down  to  document.write(Math.floor(4.8));
                                         the nearest integer.
                                                                       Output:
                                                                       4
                 pow(x,y)                Returns the value of x raised  document.write(Math.pow(2, 3));
                                         to the power of y.
                                                                       Output:
                                                                       8
                 sqrt()                  Returns  the  square root  of a  document.write(Math.sqrt(16));
                                         number.
                                                                       Output:
                                                                       4

              Example 57: To demonstrate the use of round() method

                  <!DOCTYPE HTML>
                  <HTML>
                  <HEAD>
                  <TITLE>JavaScript Round </TITLE>

                  </HEAD>
                  <SCRIPT>
                  function display() {
                  var num = 5.7;
                  var roundedNum = Math.round(num);
                  alert("Rounded number: " + roundedNum);

                  }
                  </SCRIPT>
                  </HEAD>
                  <BODY>
                  <INPUT type="button" onclick="display()" value="Generate Rounded Number">
                  </BODY>
                  </HTML>

                252   Touchpad Web Applications (Ver. 2.0)-XII
   249   250   251   252   253   254   255   256   257   258   259