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 8 of 8
  1. #1
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts

    how to convert string to num

    Hi all

    I'm trying to loop through an array to collect scores
    and then add them up, but i'm stuck because score can be
    any of the following.


    PHP Code:
          ["score"]=>
          
    string(0""
          
          
    ["score"]=>
          
    string(0"e"
          
          
    ["score"]=>
          
    string(0"3.5"
          
          
    ["score"]=>
          
    string(0"8" 

    I'm sure I need to convert string to number,
    but what about the "" and "e"?

    So far I have this

    PHP Code:
    $score += settype($value["score"], "??????"); 
    Any help appreciated

    LT
    Last edited by low tech; 07-11-2013 at 01:55 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,021
    Thanks
    15
    Thanked 239 Times in 239 Posts
    How do the strings get included with the numbers, do they mean anything, or are they just garbage? One way of converting them is to just use if and elseif, or whatever php uses for else if, and substitute a number for the value.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • Users who have thanked DrDOS for this post:

    low tech (07-11-2013)

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi DrDOS

    How do the strings get included with the numbers

    I'm working with an API

    The data is entered online in a system that keeps track of students' grades

    (engrade dot com)

    It allows nothing to be entered, e for exception, or number

    so for example, I end up with nothing or "e" or "8.5" or "7"


    I need to collect these scores and add them, e or nothing I guess can be made 0.

    How do I check and convert?

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Except for the e you would get a number if you add 0 to it - since PHP will convert a string to a number when it is used in calculations involving numbers.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    low tech (07-11-2013)

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi fegall

    In my not so great PHP ref book, I found the section that says PHP can convert string to number using for example settype()

    Are you saying it's better to simply add 0?

    Anyway --- should I first check the string for empty or e and if true just make it 0 THEN convert using settype() or by adding 0?

    Will that work with the decimal?

    I can't seem to work it out exactly

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #6
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi

    so far I have this

    Is it ok? Seems to work, but is it the right way to do it?


    PHP Code:
    if($value["score"] == "" || $value["score"] == "e")
                        {    
                        
    $myscore += 0;
                        }else 
                            {
                                
    $myscore += ($value["score"])+0;
                            } 
    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    You could do
    PHP Code:
    if( is_numeric($value['score']) )
    {
      
    $myscore += ($value["score"])+0;

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • Users who have thanked whizard for this post:

    low tech (07-11-2013)

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Easier way - type casting:

    PHP Code:
    function strtofloat($Str)
       {
       if (
    is_numeric($Str))
          {
          return (float)
    $Str//Float will handle integers and decimals
          
    }
       else
          {
          return 
    false;
          }
       }

    var_dump(strtofloat('1.25')); 
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    low tech (07-11-2013)


  •  

    Posting Permissions

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