SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)

    parseInt('08') == 0

    I assume it's treating it as octal and 8 therefore is an invalid character. However, I need to take the string '08' and convert it to decimal 8.

    Am I going to have to remove leading zeroes from the string? There must be a better way, surely?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    parseInt('08',10)// returns 8

    //convert a string to its leading decimal integer equivilent.

    parseFloat('08')// returns 8

    // convert a string to its lrading decimal equivilent

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you specify a second parameter into parseInt (of between 2 and 36) then the number in the first parameter will be assumed to be in that number base. The purpose of parseInt is to convert numbers from other number bases to base 10.

    If you don't specify a second parameter then if the number starts with 0x it will be assumed to be base 16 and if it starts with 0 not followed by an x then some browsers will assume it is base 8.
    Last edited by felgall; Aug 31, 2011 at 15:07.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Ah thanks, I guessed it was seeing it as octal but didn't know how to prevent it. Thanks

  5. #5
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of using parseInt or parseFloat you can use Number().
    Code JavaScript:
    <script type="text/javascript">
     
    var str = '08';
     
    var num = Number(str);
     
    alert(num);   // displays 8
     
    </script>

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There's a shorter way to convert a string to a number:

    num = +str;

    Simply adding a + to the front of the string variable name forces the same conversion that Number() does.

    Other alternatives are to add *1, /1 or -0 after the string variable name.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used to multiply by 1 as well early on until I became aware of other ways.

    Sure, they work and do what you want, but imho multiplying a string by a number looks a bit like a hack and "amateurish" since multiplying a string by a number doesn't make sense semantically to me.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by webdev1958 View Post
    I used to multiply by 1 as well early on until I became aware of other ways.

    Sure, they work and do what you want, but imho multiplying a string by a number looks a bit like a hack and "amateurish" since multiplying a string by a number doesn't make sense semantically to me.
    That's why I suggested the + method as the better one to use - you can consider the + in front of a string (but with no other number or variable in front of it to represent Number() as it does the same thing.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the + works but can be confusing for noobies if not used properly
    Code:
    <script type="text/javascript">
    
    var num = 23;
    var str2 = '08';
    
    alert(num+str2);  //displays 2308
    
    alert(num + +str2);  //displays 31
    
    </script>
    I'm going to stick with using Number() since the extra few characters is no big deal. Putting a + infront of a string doesn't make sense semantically to me either.


Bookmarks

Posting Permissions

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