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 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ParseInt leading 0 problem

    I am getting weird results with parseInt if the number has leading zeros

    here is the test code
    var frm = document.forms.inform;

    var instr = frm.textfield.value;

    var inint = parseInt(instr);

    var outstr = inint + " ";

    frm.textfield2.value = outstr;
    frm.textfield3.value = inint;



    input output

    098 0
    003 3
    022 18
    011 9
    067 055


    Apparently it is interpreting it as octal.

    what iam after is 003 --> 3

    045 ---> 45

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    parseInt(sNumber, nRadix);

    If you specify the radix to be 10, it will be interpreted as a decimal number instead of octal.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What you're seeing is the end result of a bad decision made early on in the development of the language. The decision was that the parseInt function would be able to automatically decide which "radix" to use for the input number based on the format of the input number unless you specified an explicit radix. You would be amazed at how many scripts have this little bug in them waiting to surface or already causing people to wonder why their numbers keep changing values.
    Check out the Forum Search. It's the short path to getting great results from this forum.


  •  

    Posting Permissions

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