SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding/Subtracting Money in php?

    How do you add and subtract $xx.xx money values using php?

    I've tried using the

    "$addition = 2 + 4; "

    type code but it doesn't seem to work with decimal points.

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it works with decimal points. Are you trying to add dollar amounts with the dollar sign attached?

  3. #3
    winter is around the corner Tomer's Avatar
    Join Date
    Jul 2005
    Location
    Israel
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
       $decimal number_format(22); 

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so?

    $decimal = number_format(4.2 + 2.2);

  5. #5
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wickedneat View Post
    so?

    $decimal = number_format(4.2 + 2.2);
    no

    Code:
    number_format(4.2 + 2.2, 2)

  6. #6
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why is there a ,2 there?

  7. #7
    winter is around the corner Tomer's Avatar
    Join Date
    Jul 2005
    Location
    Israel
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the number of digits after the decimal point [2 = X.xx, 4 = X.xxxx...etc]

  8. #8
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wickedneat View Post
    Why is there a ,2 there?
    look up number_format on the php website!@@#%@#

  9. #9
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Tomer View Post
    PHP Code:
       $decimal number_format(22); 
    You would be better off doing that only when displaying the value though. There is no need to do it when adding the number together.

  10. #10
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it possible to take the results of different recordsets and add them this way? Maybe like this...

    Code:
    $recordset1 = rs1Result
    $recordset2 = rs2Result
    $recordset3 = rs3Result
    
    $total = number_format($recordset1 + $recordset2 + $recordset3, 2);

  11. #11
    winter is around the corner Tomer's Avatar
    Join Date
    Jul 2005
    Location
    Israel
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No need? What difference does it make? Easier to do it like this. That way you can separate the programming area and the designing code.

    - Tomer

  12. #12
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lukkyjay View Post
    Is it possible to take the results of different recordsets and add them this way?
    you should be doing that in the SQL statement, not your PHP code.

  13. #13
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Tomer View Post
    No need? What difference does it make? Easier to do it like this. That way you can separate the programming area and the designing code.
    In most cases you do a calculation on the various amounts multiple times before any amount is displayed for the user, if it is even displayed.

    In these cases it would not be necessary to use the function, it would just add overhead to the script.

    We dont want lukkyjay to believe he is required to use that function each time he sums decimal numbers.

  14. #14
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you say something like

    "Add all the numbers in column"

  15. #15
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by wickedneat View Post
    Can you say something like

    "Add all the numbers in column"
    If you want to sum together all the numbers in the table for a specific column etc you can use SUM().

    Examples:
    SELECT SUM(amount) as total FROM table
    SELECT SUM(amount) as total FROM table WHERE userid=1
    SELECT year, SUM(amount) as total FROM table WHERE userid=1 GROUP BY year

    You should read up on the math functions available for mysql (or the database type you use)
    Last edited by TheRedDevil; Dec 31, 2007 at 12:38. Reason: Added some more clarification to the example

  16. #16
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    sure.
    PHP Code:
    $sql "SELECT SUM(columnname) FROM tablename";
    $query mysql_query($sql);
    $total mysql_result($sql0); 
    edit: looks like reddevil beat me to it
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  17. #17
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there something I'm missing?

    PHP Code:
    <?php
        session_start
    ();
        include(
    "constring.php");
    ?>

    <?php
    $sql 
    "SELECT SUM(amount) FROM income"
    $query mysql_query($sql); 
    $total mysql_result($sql0); 

    echo 
    "$total"
    ?>

  18. #18
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes, you're not telling us what the problem is.

  19. #19
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha! I'm getting this error sorry:

    Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/url/public_html/ledger/index.php on line 9

  20. #20
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    My mistake. Sorry about that:
    PHP Code:
    $total mysql_result($query0); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  21. #21
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $total = mysql_result($query, 0);

  22. #22
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, that took care of it.

  23. #23
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    show difference of two colums to display in 3rd in dollar amount

    $result = mysql_query("SELECT * FROM table")
    or die(mysql_error());
    $row = mysql_fetch_array( $result );

    I am trying to create a table where it shows Total Bill and Total Payment and Balance.
    I want to find difference from first two columns (subtract) and display in the 3rd column. how can i do that.
    please let me know, i am new to php and mysql.
    thanks.

  24. #24
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a slightly oldish thread, but since nobody mentioned it, I'm going to anyway. Never, ever use floating point values to represent money. Really.
    In PHP that means that you shouldn't do the following:
    PHP Code:
    number_format(4.2 2.22
    It won't give you the result, you expect. Instead, use cents (or what ever is the smallest unit in the relevant currency) as the main unit and store it as an integer. Do all calculations in this unit, keeping the type as integer, and only convert (Eg. divide by 100) when presenting the value.

    The advice to use decimal as column value in MySql is fine, since decimal isn't a floating point value. Just be ware, that it may be implicitly cast to a floating point in PHP. Therefore, multiply it by 100 in the SQL-query, so you're sure to get an integer out.

  25. #25
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i stored in MySql the dollar amount as DECIMAL. in PHP it prints as the way i stored (eg. 1200.00).
    but my problem is that, how to do subtraction from one column from another one.
    and display or store to the 3rd column as i mentioned in my first post.
    Thanks for the quick response.
    mhb


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
  •