SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question String to number problem

    hi there
    What I am doing is making a site interact with google maps via the javascript API.
    I am having an issue with converting a set of coordinates from a string to a number.
    The string I receive after I receive them from the json string is formatted like this "(-28.45417, 153.33635)" that's every thing inside the quotation marks including brackets.
    So I tried spitting the string at the "," and then Farray=parseFloat()

    then on the Farray[0] and Farray[1]
    It appears Farray[0] still works out to be NaN though parseFloat() did get the correct longitude which is Farray[1].
    Can anyone see how I get the Farray[0] to be a number?

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by firehawk777 View Post
    hi there
    What I am doing is making a site interact with google maps via the javascript API.
    I am having an issue with converting a set of coordinates from a string to a number.
    The string I receive after I receive them from the json string is formatted like this "(-28.45417, 153.33635)" that's every thing inside the quotation marks including brackets.
    So I tried spitting the string at the "," and then Farray=parseFloat()

    then on the Farray[0] and Farray[1]
    It appears Farray[0] still works out to be NaN though parseFloat() did get the correct longitude which is Farray[1].
    Can anyone see how I get the Farray[0] to be a number?
    Farray[0] will contain (-28.45417 , which cannot be evaluated as a number.

    Try:
    Code:
    Farray[0] = Number( Farray[0].replace( /[^\d\.\-\+]/g, "" );
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks I solved this by using PHP before the coods were sent :
    PHP Code:
    $coords trim($coords"(..)"); 
    though your method would also work.


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
  •