JavaScript Tutorial/Math/PI
Math.PI
The PI property of the Math object is used to get the constant PI.
This is approximately 3.14159.
<html>
<body>
<script language="JavaScript">
<!--
function doMath(){
var result = Math.PI;
document.form1.answer.value = result;
}
-->
</script>
<form name=form1>
The Approximate value of PI is:
<input type="text" name="answer" size=20>
<input type="button" value="Calculate" onClick="doMath()">
<br>
</form>
</body>
</html>
The value of the constant PI: Math.PI
<html>
<head>
<title>The Properties of the Math object</title>
<script type="text/javascript" language="javascript">
<!-- //
function DisplayMath(){
document.write("<br/><P>The value of the constant PI is: " + Math.PI + "</p>");
}
// -->
</script>
</head>
<body onload="DisplayMath()">
</body>
</html>
Use Math.PI to do calculation
<html>
<head>
<title>Find the area and circumference of a circle</title>
<script type="text/javascript" language="javascript">
<!-- //
function FindArea(){
var number = new Number(123);
document.write("<P>You entered a radius of " + number + "</p>");
document.write("<P>The area of the circle is " + (number * number * Math.PI) + "</p>");
document.write("<P>The circumference of the circle is " + (2 * number * Math.PI) + "</p>");
}
// -->
</script>
</head>
<body onload="FindArea()">
</body>
</html>