www.webdeveloper.com
Results 1 to 5 of 5

Thread: Math.round

  1. #1
    Join Date
    Dec 2007
    Location
    charleston sc
    Posts
    1

    Math.round

    I am trying to use this script and it works except for the Math.round? I don't want the number with decimal(s).


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Untitled Document</title>

    </head>



    <body>

    <SCRIPT LANGUAGE="JavaScript">



    <!-- This script and many more are available free online at -->

    <!-- The JavaScript Source!! http://javascript.internet.com -->



    <!-- Begin

    var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

    function countup(yr,m,d) {

    var today=new Date();

    var todayy=today.getYear();



    if ((navigator.appName == "Microsoft Internet Explorer") && (todayy < 2000))

    todayy="19" + todayy;

    if (navigator.appName == "Netscape")

    todayy=1900 + todayy;



    var todaym=today.getMonth();

    var todayd=today.getDate();

    var todaystring=montharray[todaym]+" "+todayd+", "+todayy;

    var paststring=montharray[m-1]+" "+d+", "+yr;

    var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1/365);



    document.write("" + difference + "");



    }

    countup(1995,08,23); // Date in format: (year,month,day)

    // End -->

    </script>



    <p><center>

    <font face="arial, helvetica" size="-2">Free JavaScripts provided<br>

    by <a href="http://javascriptsource.com">The JavaScript Source</a></font>

    </center><p>



    <!-- Script Size: 0.99 KB -->

    </body>

    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    Try using .toFixed(0) instead.
    See: http://www.pageresource.com/jscript/j_a_03.htm

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    toFixed() method has, for IE, probably a bug in it's implementation when dealing with small floated numbers.

    Try this both in Moz and IE:
    Code:
    var x=0.09;
    var y=0.099;
    alert(x.toFixed(1));
    alert(y.toFixed(1));
    Moz will return correctly 0.1 for both numbers, while IE will return 0.0 for the first one.

    So that, whenever is possible, avoid using toFixed() when you can use safer methods.

    newatjava

    Math.round() should work; if it does not as expected, in is not its fault, there must be another error somewhere else.

    By the way, the code is old. You should use the crossbrowser getFullYear() method to avoid the Y2K "bug".

    And don't use XHTML Doctype, unless you really need to handle/import XML files. Under XHTML DTD you must isolate the javascript embedded codes inside CDATA islands.
    Last edited by Kor; 12-14-2007 at 04:10 AM.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Question Question

    Kor:

    Is there a problem with using x = value.toFixed(0)?
    Or is it better to use x = parseInt(value)?

    (This assumes he wanted no decimals in the final display as requested)

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    But I see no reason for not using only Math.round(). After all, that is exactly what that method does: rounds the floated number to an integer (no decimals, then)... What is the use of toFixed() in all these?

Thread Information

Users Browsing this Thread

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

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