SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast underzen's Avatar
    Join Date
    Apr 2004
    Location
    Ft. Lauderdale, FL
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String value convert to number

    So here's my question. I have a variable that is pulled from an XML feed.
    $xml->CARRIER[0]->RATE;

    This is actually a string that when echo'd contains: $321.20

    I need to convert this string into a float number so that I can do some addition and subtraction with it.

    Any ideas?

    Thanks in advance!


  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    var_dump( (float)$xml->carrier[0]->rate );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast underzen's Avatar
    Join Date
    Apr 2004
    Location
    Ft. Lauderdale, FL
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    var_dump( (float)$xml->carrier[0]->rate );

    Thanks!

    I did this however this is what is displayed: float(0)

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't convert it to a float unless the string content is_numeric(). A $ sign is not numeric.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2007
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're looking to just strip the $ I'm presuming

    Code:
    $xml->CARRIER[0]->RATE = ltrim($xml->CARRIER[0]->RATE, '$');

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $var=ltrim("$123.20",'$');

    $var=  (float) $var;

    var_dump$var); 
    Now you end up with a float, 123.2 but not a money value-but that's not what you want till you finally finish with your computations, right?


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
  •