Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Unhappy really confused!!

    var n = 123456.789;
    n.toFixed(0);


    just starting out JavaScript. So i type the above code in firebug. it returns this result:- "123457"
    As for as i know, toFixed() converts a number to a string with a specified number of digits after the decimal point.

    If i have specified 0, which i'm assuming it means 0 digits after the decimal point, why does the 7 appear and the 6 is taken out.

    really fustrating and can't seem to get my head over it.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    The toFixed() method formats a number to use a specified number of trailing decimals.

    The number is rounded up, and nulls are added after the decimal point (if needed), to create the desired decimal length.

    from w3schools

    google is your friend

  • Users who have thanked DaveyErwin for this post:

    laylo (09-11-2011)

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,027
    Thanks
    36
    Thanked 494 Times in 488 Posts
    To verify the effects of the rounding, try:
    Code:
    var n = 123456.789;
    n.toFixed(1);

  • Users who have thanked jmrker for this post:

    laylo (09-11-2011)

  • #4
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    just to verify, JS rounds only if that number if 5 or above depending on what value you input to the toFixed() method
    Last edited by laylo; 09-11-2011 at 09:08 AM.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,027
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Arrow

    Quote Originally Posted by laylo View Post
    just to verify, JS rounds only if that number if 5 or above depending on what value you input to the toFixed() method
    Yes, whether that is a statement or a question.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,774
    Thanks
    55
    Thanked 518 Times in 515 Posts
    well, to be finicky it always rounds - but whether it rounds up or down depends on the last decimal it looks at.

    if you want to keep your number and just get rid of the decimals you can use parseInt:

    Code:
    var n = 123456.789;
    y=parseInt(n);
    alert("Int= "+y);

  • Users who have thanked xelawho for this post:

    laylo (09-22-2011)

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,027
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Arrow

    To NOT round, you could:
    Code:
    <script type="text/javascript">
    var n = 123456.789;
    n = parseInt(n*10)/10
    alert(n);
    
    n = 2/3;
    n = parseInt(n*100)/100;
    alert(n);
    </script>

  • Users who have thanked jmrker for this post:

    laylo (09-22-2011)


  •  

    Posting Permissions

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