www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] php eqautions

  1. #1
    Join Date
    Oct 2013
    Posts
    16

    resolved [RESOLVED] php eqautions

    Greetings so hear is my delema have a page that uses following

    <?php echo number_format($data['value']*0.0321507466*5*0.35, 2);?> where $data['value'] = 19.54 and outputs the result as 1.10 which is correct.

    how ever if i change it to say

    <?php echo number_format($data2['value']*0.0321507466*1.672*0.9, 2);?> and $data2['value'] = 1,291.30 the output is displaying 0.05 when it should be reading 62.47

    not sure why this is happening anyone have a idea as to why this would be? should the $data2['value'] = 1,291.30 not have a ' , ' in it and if so any clue how to get rid of it?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    Yes, the comma will cause a problem, as PHP numeric types to not any commas. In this case, it's considered a string, and when it tries to type-cast it to a number, it only reads the first character (the "1"), then stops when it reaches anything that is not a number of a decimal point (the comma).

    You could get rid of any commas with a simple str_replace() call.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2013
    Posts
    16
    Thanks so how would i work it into the code above. should i put it in the file that contains the code for $data['value']? just googled the function dont understand how it works.

  4. #4
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    You could do something like this just before you perform your calculations:

    PHP Code:
    if(strpos($data2['value'], ",") !== false) {
        
    $data2['value'] = (float) str_replace(","""$data2['value']);

    In words, it checks whether there is a comma anywhere in $data2['value'] and if so, it replaces it with "" (nothing). Since it's still technically a String, the result is then explictly casted to a float so you can use it for calculations. PHP probably will implicitly cast it for you but I like being specifying it.
    Last edited by Error404; 04-25-2014 at 07:29 AM.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    If you don't want to mess with the source data and may need to use the "fixed" version in more than one place, you could create a separate variable:
    PHP Code:
    $data2Float = (float) str_replace(','''$data2['value']); 
    ...then use $data2Float in your calculation instead of $data2['value'].

    Alternatively, if that one calculation is the only place you use it, I'd probably just stick it in-line:
    PHP Code:
    <?php
    echo number_format(((float) str_replace(','''$data['value'])) * 0.0321507466 0.352);
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles