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.
| SitePoint Sponsor |




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.
it works with decimal points. Are you trying to add dollar amounts with the dollar sign attached?




PHP Code:$decimal = number_format(4 + 2, 2);




so?
$decimal = number_format(4.2 + 2.2);




Why is there a ,2 there?




It's the number of digits after the decimal point [2 = X.xx, 4 = X.xxxx...etc]
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);




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





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.




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
sure.
edit: looks like reddevil beat me to itPHP Code:$sql = "SELECT SUM(columnname) FROM tablename";
$query = mysql_query($sql);
$total = mysql_result($sql, 0);
![]()
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona




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($sql, 0);
echo "$total";
?>

yes, you're not telling us what the problem is.![]()




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
My mistake. Sorry about that:
PHP Code:$total = mysql_result($query, 0);
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
$total = mysql_result($query, 0);




Thanks guys, that took care of it.
$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.
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:
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.PHP Code:number_format(4.2 + 2.2, 2)
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.
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