Announcement

Collapse
No announcement yet.

bug with parseInt()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • bug with parseInt()

    When passing certain left, zero-padded numbers to parseInt(), the function returns either zero or the wrong number...EFS script included.

    David
    eSignal v10.1.1184.1082 Beta
    Attached Files

  • #2
    Re: bug with parseInt()

    David
    Here is an excerpt from an article on the parseInt bug in JavaScript. For your information I found it by running a search on the net for parseInt bug
    Alex


    There is a "bug" with the parseInt JavaScript function.

    The bug is that parseInt can return an incorrect value. For example, parseInt("08") results in 0 instead of 8. And parseInt("09") results in 0 instead of 9. The reason for this is because the zero in front is indicating that this is an octal (base 8) number, and "08" and "09" are not valid octal numbers. The bug does not exist with parseFloat.

    Keep in mind that this bug only happens when the value being checked is a string and only when the string starts with a leading zero. To resolve the bug, use one of the following two techniques.
    The "10" in the second example indicates that base-10 values should be used.
    PHP Code:
    parseInt(parseFloat(<my text value>))

    parseInt(<my text value>, 10

    Originally posted by dlee8989
    When passing certain left, zero-padded numbers to parseInt(), the function returns either zero or the wrong number...EFS script included.

    David
    eSignal v10.1.1184.1082 Beta

    Comment


    • #3
      Alex,

      Awesome investigative work! Didn't even think to search the 'net...was relying on the EFS doc. Will keep this in mind for future reference...for now, I just wrote a quick routine to strip out any left-padded 0's! Also, after I posted, I modified the test script to use parseFloat() and noticed it ran properly.

      Regards,

      David

      Comment

      Working...
      X