Java Reference
In-Depth Information
<head>
<title>Chapter 5: Example 4</title>
</head>
<body>
<script type=”text/javascript”>
var myNumber = prompt(“Enter the number to be rounded”,”“);
document.write(“<h3>The number you entered was “ + myNumber + “</h3><br />”);
document.write(“<p>The rounding results for this number are</p>”);
document.write(“<table width='150' border='1'>”);
document.write(“<tr><th>Method</th><th>Result</th></tr>”);
document.write(“<tr><td>parseInt()</td><td>”+ parseInt(myNumber) +”</td></tr>”);
document.write(“<tr><td>ceil()</td><td>” + Math.ceil(myNumber) + “</td></tr>”);
document.write(“<tr><td>floor()</td><td>”+ Math.floor(myNumber) + “</td></tr>”);
document.write(“<tr><td>round()</td><td>” + Math.round(myNumber) +”</td></tr>”);
document.write(“</table>”)
</script>
</body>
</html>
Save this as ch5_examp4.htm and load it into a web browser. In the prompt box, enter a number, for
example 12.354 , and click OK. The results of this number being passed to parseInt() , ceil() ,
floor(), and round() will be displayed in the page formatted inside a table, as shown in Figure 5-4.
Figure 5-4
The fi rst task is to get the number to be rounded from the user.
var myNumber = prompt(“Enter the number to be rounded”,””);
Search WWH ::




Custom Search