Java Reference
In-Depth Information
Ceil()
returns
parameter
parseint()
returns
floor()
returns
round()
returns
10.25
10
11
10
10
10.75
10
11
10
11
10.5
10
11
10
11
-10.25
-10
-10
-11
-10
-10.75
-10
-10
-11
-11
-10.5
-10
-10
-11
-10
Note
Remember that
parseInt()
is a native JavaScript function, not a
method of the
Math
object, like the other methods presented in this table.
trY it out
rounding Methods results Calculator
If you're still not sure about rounding numbers, this example should help. Here, you look at a calculator
that gets a number from the user, and then writes out what the result would be when you pass that
number to
parseInt()
,
ceil()
,
floor()
, and
round()
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 5, Example 4</title>
</head>
<body>
<script>
var myNumber = prompt("Enter the number to be rounded","");
document.write("<h3>The number you entered was " + myNumber +
"</h3>");
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, 10) + "</td></tr>");
document.write("<tr><td>ceil()</td><td>" + Math.ceil(myNumber) +
"</td></tr>");
document.write("<tr><td>floor()</td><td>"+ Math.floor(myNumber) +