parseInt & ParseFloat

Discussion in 'JavaScript' started by gobbly2100, Oct 1, 2007.

  1. #1
    Hey,

    I can't quite understand how these 2 functions work and what they are supposed to do, I have had a browse on some sites but really just don't understand it so I am hoping someone can explain better to me how they work and what they do.

    Thanks in advance!
     
    gobbly2100, Oct 1, 2007 IP
  2. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #2
    Syntax
    parseInt(numstring, [radix])
    The parseInt method syntax has these parts:
    + numstring: Required. A string to convert into a number.
    + radix: Optional. A value between 2 and 36 indicating the base of the number contained in numstring. If not supplied, strings with a prefix of '0x' are considered hexidecimal and strings with a prefix of '0' are considered octal. All other strings are considered decimal.

    The parseInt method returns an integer value equal to the number contained in numstring. If no prefix of numstring can be successfully parsed into an integer, NaN (not a number) is returned.

    parseInt("abc") // Returns NaN.
    parseInt("12abc") // Returns 12.
    You can test for NaN using the isNaN method.
     
    temp2, Oct 1, 2007 IP
  3. SEOBusiness

    SEOBusiness Well-Known Member

    Messages:
    3,046
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    185
    #3
    See post here (http://www.codingforums.com/showthread.php?t=39075)

    a small addenda:

    parseInt() autodetects the hexa and octa numbers, so that it does not always return the values as they were in decimal base. A lot of people forget that parseInt() may be used along with it's parameter base.

    parseInt(string,base)

    So that, to be sure that parseInt('0112') will return 112 as we could expect in decimal, you must specify that the base is 10, otherwise it will return 74

    parseInt('0112',10) //will return 112
    parseInt('0112') //will return 74

    You may see now that parseInt(variable,base) can be used as well for number translation from any base to the decimal base

    for instance:
    parseInt('ff',16) // will translate the hexa number 'ff' into it's decimal equivalent 255


    The parseFloat() function parses a string and returns a floating point number.

    This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.

    Hope it is helpful for you:D
     
    SEOBusiness, Dec 25, 2007 IP
  4. MyDVDz

    MyDVDz Member

    Messages:
    35
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    also see: developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:predefined_Functions:parseInt_and_parseFloat_Functions
     
    MyDVDz, Dec 26, 2007 IP