Tag Archives: Math in JavaScript

Calculate ceiling of the any number through math.ceil() function

 
Math.ceil function returns the smallest integer greater than or equal to its numeric argument.
You can use this function as :
Math.ceil(number)
where number is the number.

The return value is an integer value equal to the smallest integer greater than or equal to its numeric argument.

Example

<html>
  <body>
    <script language="JavaScript">
    <!--
    function CalculateResult(){
        var Number1=document.form1.txtNumber1.value; 
        var result = Math.ceil(Number1);
        document.form1.answer.value = result;
    }
    -->
    </script>
    <form name=form1>
    Enter number:
    <input type="text" name="txtNumber1">
    <br>    
    <input type="button" value="Calculate" onClick='CalculateResult()'>
    <br>
    result :
    <input type="text" name="answer" size=10>
    </form>
  </body>
</html>

Math.exp Function in JavaScript

 
math.exp function in javascript returns e raised to a power.
You can use this like :
Math.exp(number)
where number is a numeric expression representing the power of e.

Note: constant e is Euler’s number, approximately equal to 2.71828 and number is the supplied argument.

<html>
  <body>
    <script language="JavaScript">
       <!--
          function CalculateResult(){
          var Number1=document.form1.txtNumber1.value; 
          var result = Math.exp(Number1);
          document.form1.answer.value = result;
          }
      -->
    </script>
    <form name=form1>
     Enter power of e:
    <input type="text" name="txtNumber1">
    <br>    
    <input type="button" value="Calculate" onClick='CalculateResult()'>
    <br>
     result :
    <input type="text" name="answer" size=10>
    </form>
  </body>
</html>

How to use math.pow() method in javaScript

 
you can calculate the value of a base expression taken to a specified power with the help of math.pow() function. you can use this method like as:
Math.pow(base, exponent)
where base is the base value and exponent is the exponent value of the expression.

Example:

<html>
  <body>
    <script language="JavaScript">
    <!--
    function CalculateResult(){
        var Number1=document.form1.txtNumber1.value;
        var Number2=document.form1.txtNumber2.value;
        var result = Math.pow(txtNumber1, txtNumber2);
        document.form1.answer.value = result;
    }
    -->
    </script>
 
    <form name=form1>
    Enter Base number:
    <input type="text" name="txtNumber1">
    <br>
    Enter exponent :
    <input type="text" name="txtNumber2">
   <br>
    <input type="button" value="Calculate" onClick='CalculateResult()'>
    <br>
    result :
    <input type="text" name="answer" size=10>
    </form>
  </body>
</html>

Math.round() method in JavaScript

In JavaScript math.round() method rounds a number to its nearest integer value.
Suppose If the fractional portion of the number is .5 or greater, the result is rounded to the next highest integer and if the fractional portion of number is less than .5, the result is rounded to the next lowest integer. for example rounding value of 2.6 is 3 and rounding value of 2.4 is 2.

In negative numbers, if the decimal portion is exactly -0.5, the return value is the smallest integer that is greater than the number.For example, Math.round(5.5) returns 6, but Math.round(-5.5) returns -5.

You can use round() method as :

Math.round(number)
where number is the number for which the absolute value is needed

Example 1

In this example we divide 25 to 4, we know that the exact result will be 6.25 but when we use round() function then result will be 6. see below:

<html>
<head>
<title>round method</title>
</head>
 <body >
    <script type="text/javascript">
        var number1 = 25;
        var number2 = 4;
        var result ;
        result = number1 / number2;
        document.write(Math.round(result));
    </script>
 </body>
</html>

Example 2

See another example with output:

   <HEAD>
   <TITLE>
       use of round function in javaScript
   </TITLE>
   </HEAD>
   <BODY>
    <script type="text/javascript">
	document.write(Math.round(16.84) + "<br />");
	document.write(Math.round(.50) + "<br />");
	document.write(Math.round(0.48) + "<br />");
	document.write(Math.round(-2.46) + "<br />");
	document.write(Math.round(-9.51)+ "<br />");
	document.write(Math.round(9.51));
    </script>
   </BODY>
</HTML>

Output will be:
17
1
0
-2
-10
10

How to calculate absolute value of number in JavaScript

 
You can calculate absolute value of a any number with the help of math.abs() method in JavaScript
You can use this methods as :
Math.abs(number)
where number is number for which the absolute value is needed.

see this :

<script type="text/javascript">
     document.write(Math.abs(23.7) + "<br />");
     document.write(Math.abs(-23.7) + "<br />");
     document.write(Math.abs(5-10));
     document.write(Math.abs(null) + "<br />");
</script>
 
The output  will be:
23.7
23.7
5
0

Example:

The following example create a form with two textbox and one button, when user click on button ‘txtResult’ shows absolute value according to enter number in ‘txtNumber’ number.

   <html>
    <body>
    <script language="JavaScript">
    	<!--
   	 function GetAbsolute()
	 {
       	    var number=document.form1.txtNumber.value;
            var absValue = Math.abs(number);
       	    document.form1.txtResult.value = absValue 
   	  }
        -->
    </script>
 
    <form name=form1>
    	 Enter Number:
   	 <input type="text" name="txtNumber" size=10>
   	  <br>
  	  Absolute Value:
   	 <input type="text" name="txtResult" size=10>
	  <br>
	 <input type="button" value="Find absolute" onClick='GetAbsolute()'>
    </form>
    </body>
 </html>
 

How to get the maximum value among two numbers in JavaScript

 
In JavaScript you can use the math.max() method for finding largest number among two or three numbers

Example:

<html>
   <head>
     <title>How to get the maximum value among two numbers in JavaScript</title>
       <script language="JavaScript">
          <!--
            number1 = 56;
            numner2 = 43;
            document.write("The largest number among these two numbers is " + Math.max(number1 , number2));
          //  -->
       </script>
    </head>
<body>
</body>
</html>

How to get the minimum value among two numbers in JavaScript

 
In JavaScript you can use the math.min() method for finding smallest number among two or three numbers

Example:

<html>
   <head>
     <title>How to get the minimum value among two numbers in JavaScript</title>
       <script language="JavaScript">
          <!--
            number1 = 23;
            numner2 = 34;
            document.write("The smallest number among these two numbers is " + Math.min(number1 , number2));
          //  -->
       </script>
    </head>
<body>
</body>
</html>

How to find the maximum number in JavaScript

 
In JavaScript you can use the max() method that returns the number with the highest value.
You can use math.max function like as:
Math.max(number1, number2. . . ,numberN)
where number1, number2. . . ,numberN are the optional arguments If no arguments are given, the return result is -Infinity

Example:

<html>
   <head>
     <title>How to use math.max in javascript</title></head>
       <body>
           <script type="text/javascript" language="javascript">
                var MaxNum = Math.max(134,299,38,546);
                document.write(MaxNum);
           </script>
      </body>
</html>