www.webdeveloper.com
Results 1 to 2 of 2

Thread: Math() problem or browser's?

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    Math() problem or browser's?

    hello. i am currently learning javascript out of a book and i use to write the code samples so as to get attached. I have just run up to something weird though. the below code:

    Code:
    <html>
    <body>
    <script language="Javascript" 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>
    would return NaN where Math.ceil(myNumber), Math.floor(myNumber), Math.round(myNumber) code parts are. i tried to play with some more coding just to check whether problem lies, and it seems that the "bug" is in the Math function. What do you think? Is there something wrong with the code? am i missing something? the book was written in 2007. would there be some deprecations in the code it presents? Is there something wrong with my browser specifically? Can you please give me some help?

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    prompt returns a string. Convert it to a number first.
    Number, parseInt, parseFloat or force type (+myNumber)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles