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 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts

    why does this return 8?

    Hi, please can you help

    Why do these return 8 and not 10?

    how do I make 010 turn into 10 or 00000012 turn into 12


    Code:
    Number(010); // return 8
    parseInt("010"); //returns 8

    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by code-in-time View Post
    Hi, please can you help

    Why do these return 8 and not 10?

    how do I make 010 turn into 10 or 00000012 turn into 12


    Code:
    Number(010); // return 8
    parseInt("010"); //returns 8

    Thanks
    Because when you use parseInt() you must specify the radix. The default is normally decimal, but if a number-string begins with 0 it is interpreted as octal.

    alert (parseInt("010",10)); //returns 10
    alert (parseInt("000000012",10)); // returns 12

    Likewise, Number(010) returns 8 (decimal) because the zero prefix means it is (correctly) interpreted as octal.

    You should never precede a number with a zero unless you are specifically looking for an octal conversion!

    It's a classic case of throwing out the baby with the dishwasher. Caller, BBC Radio 4

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    code-in-time (06-22-2012)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    Likewise, Number(010) returns 8 (decimal) because the zero prefix means it is (correctly) interpreted as octal.
    Only in some browsers and only if you are not running the JavaScript in Strict mode.

    In strict mode all the octal defaults are turned off because they were only implemented that way in some browsers and so some browsers report '080' as 8 while others report it as 10. Once all browsers support Strict mode then you will be able to always get 10 as the result (not that you should be using parseInt to convert it in that instance anyway.

    parseInt is for converting numbers to base 10 so where it is supplied with a number that it recognises as being in a different number base it will do what it is intended for and convert it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    code-in-time (06-23-2012)

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Code:
    "00000012 "*1 == 12 //true
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    code-in-time (06-23-2012)

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rnd me View Post
    Code:
    "00000012 "*1 == 12 //true
    That's because you don't have it wrapped inside a parseInt call in an attempt to convert from one number base to another.

    Code:
    parseInt("00000012 ") == 12 //sometimes true
    parseInt("00000012 ") == 10 //sometimes true
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    code-in-time (06-23-2012)


  •  

    Posting Permissions

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