Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    PHP


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





PHP Math Calculation


I am trying to perform a simple calculation from variables passed from a form. The code is as follows:

<?php
echo "Bonus for the year: <b>";
$x = (($awage * $nibt) + ($awage * $ratio));
$x = number_format($x, 2);
print $x;
echo "</b><p>";
$xtmp = ($x + $qtr);
print $xtmp;?>

The calculation multiplies two different values (based on values from the previous page), and then sets it equal to $x. The first calculation prints fine, but when I try to add it to $qtr (also passed via radio button from previous page), it doesn't calculate correctly.

I found that if I hard-code the values for $x and $qtr after I print $x, then it will calculate correctly. I am thinking that I am using the variables incorrectly somewhere.




View Complete Forum Thread with Replies

Related Forum Messages:
Math
I'm doing some php math and need to divide a floating point number
variable by only 4 decimal places - when I do the following:

$aspect_ratio = 942 / 760;
$new_height = $target_width * $aspect_ratio;

In PHP $aspect_ratio claculates to 1.2394736842105 but for my purposes
I need only 1.2394

How can I limit the decimals to just 4 places?

View Replies !
Php Math Is Off
need to: find 1% of the total

example:
500.00 should be 5.00

in php, it's coming off as 7.07

code:
$query = "SELECT employee, SUM(balance) FROM commission where `date` < DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY employee";

$total_sold = $row['SUM(balance)'];
$percentage = ($total_sold / &#3999;');
echo number_format($percentage,2);

what am i doing wrong?

View Replies !
Using Math 'ln' In Php
i have the following formula:

$rating = -(ln(($points/2894) - 1) - 5.56)/0.0025;

that formula is the reverse engineer of another one i had someone do, I dont personally know the math.  According to him the ln "is the natural logarithm. i.e. log to the base e."  whatever that means.

View Replies !
Need Some Serious Math Libaries
I'm in dire need of some php libraries / classes. Specifically:

- basic statistical/probability calculations
- prefferably more advanced ones that can deal with asymetric normal distributions
- a linear programing solver


I highly doubt the last two exist in php, but maybe somebody knows of a way of using some other GPL software to solve linear progaming problems or do some more advanced statistical calculations and then somehow access them through PHP (maybe WDDX?)

It seems there are a few open source LP solvers written in C. Any ideas on how i might interface with them?

View Replies !
Any Math Guru's Out There?
I am not good at math, if you can find a dumbed down tutorial on this
I'd appreciate it. I am trying to come up with a function that will
find the distance between two points within a cube.

The function eventually will be fed three co-ordinates, x y & z.

Point 1 could be 290 : 195 : 132
Point 2 could be 136 : 201 : 8

Has anybody done this before? If so can you throw this poor dog a bone,
I can code PHP and anything just fine, alas my poor math is letting me
down on this one. (I've checked out the math functions in PHP hoping
this had already been done to no avail).

View Replies !
A Math Puzzler For You
I have a php code that generates numbers. sometimes is generates whole
numbers but most of the time decimals. How can I, when it generates a
whole number, convert the number to n.0
where n is the whole number?

For example 23 := 23.0

View Replies !
Math Logic
I'm writing a script to do some calculations on gambling odds. I don't have a direct PHP question, but a math question related to my script. There are some smart people around here so I thought I'd ask. If someone can answer my question and explain the logic that would be great.

The house edge is 1.41% and a winning bet pays 2:1. However, for every $1 lost on the game, the player only has to pay $.55. Given these circumstances, what is the modified house edge?

View Replies !
Check My Math!
Not sure about you, but when I run this, it tells me 59001.31 doesn't
equal 59001.31. Change each side of the equation by a hundreth or two
and it checks. Change it a bit more, and it won't.

_What_ is going on here?!

<?php
$subtotal=59001.31;
$principal=58605.33;
$interest=395.98;
$check=$principal+$interest;
echo("<pre>");
echo("Check: " . $check . "
");
echo("Subtotal: " . $subtotal . "
");
if($check!=$subtotal) {
echo("Check (" . $check . ") does not equal subtotal (" .
$subtotal . ")"); // Check doesn't equal subtotal
}
else {
echo("Check (" . $check . ") equals subtotal (" . $subtotal .
")"); // Check does equal subtotal
}
echo("</pre>");
?>

View Replies !
Math Formula
I am trying to put the following math formula in php, but I know to little
of math to do it I guess, maybe somebody can help me out.

This is the formula:

x = (-ln(1) - ln(y/100))*100*z

The values for y and z will be submitted via a form.

View Replies !
Basic Math
<?php
$variable = "random number here";
$percentage = $variable - 10%;
// $percentage allways will contain 90% of whatever the monetary value was in variable
?>
Is the above correct, and 100% accurate?

View Replies !
Math Problem
I cannot add say 1,000 and 1,000

$total = '1,000' + '1,000';
when you echo $total it comes out '2'

yet if I do $total = '1000' + '1000'; it echos out '2000' like it should, one problem. All my pricing values have commas in them. Almost all if not all will always contain a comma and NOT a period. Just wondering how in the check can I remove the comma, add the values, then replace it?

BTW, it's for a profit/loss chart. It scans a list a purchased and sold vehicles and does some math and spits out totals. Values are stored as a VARCHAR(20) in the database. Not sure what needs to be done, not sure if storing the numbers different would fix the problem or what.

View Replies !
Doing Math With Timestamp
got a DB with TimeIn and TimeOut Fields, they are set up as INT and am using strtotime when these fields are populated.

I can get the math to find out the amount of time allotted between the two times

<SNIPPET>
$doseconds = $row_getLastPunch['TimeOut'] - $row_getLastPunch['TimeIn']
</SNIPPET>

Now I know to take the number and divide by 60 to get the minutes, and then another 60 to get the hours.

here  is where my problem starts and my skill level stops.

Say I have a final answer that breaks down to 98 minutes (5880 seconds) when I go to get the hours (98 divide by 60) I get '1.6333~ hours' which is correct math, I would rather it display '1:38 hours'  or something close to that.

View Replies !
Math Verification
I have this form written below. it is suppose to check the simple math verification and send the content once the answer is right. The verification works but as i'm not able to put things together I'm unable to get it right. Even if the sum is wrong, i get the mails. Code:

View Replies !
Math Percentage
How can I convert a number that represents a percentage such as 14.00 to .14? Or if it was 13.50 to .1350.

View Replies !
Easy Math
How can I add a number up when I am in a while()? Lets say that there are 4 entries for $myrow[0]. 10, 20, 30 and 40. The total of those numbers is 100 right? So how do I output that? PHP Code:

$result = mysql_query("SELECT * FROM yada  ORDER BY id DESC",$db);
while ($myrow = mysql_fetch_row($result)) { 
    
    $totalofmyrow = $myrow[0] + itself?????
}

echo $totalofmyrow;

View Replies !
A Math Question?
here is my idea, when a user click on a link to page 'get_prize.php' he has 10% of chance to get a bucket of gold or go home(90%). How do I get the result? Do I need another variable?

$chance = 0.1;
$prize = 'A bucket of Gold'
$no_prize = 'Go Home'

$result = ''

View Replies !
Math Functions
I have 2 variables loaded in from a database,
$stars
$votes
and 1 variable passed on from flash
$rating

How can i
1 - avrege together stars with rating (stars+rating/2)
2 - add 1 to rating

View Replies !
Math Operations
With code such as the example below, how do I get PHP to round the output to 2 decimal places (it is a price)?

<?php echo $_POST["price"]*0.9; ?>

View Replies !
Math Without Eval()
Is it possible to have PHP perform mathematical operations on a simple stored formula without invoking eval()?

For instance, if I have the string "18 - (18 * 2) - 1" could I have PHP calculate the math in this string without having resort to eval('$math = 18 - (18 * 2) - 1;'); ?

View Replies !
Math At Runtime
A script I'm working on, takes values from a form in page1. These values are sent to page2, where the user can input a complex mathematical equation operating upon these values.

Page1:

Length: [TEXT BOX]
Width: [TEXT BOX]
Heigth: [TEXT BOX]
..
..

Page2:

Equation: 50+((length)+4*(width)-(4*height)))... and so on.. Code:

View Replies !
Math On Includes
Through a script I'm writing, I am ending with some .php files that are just plain numbers. Is it possible to use the math functions and add the numbers from the includes together?

<?php
$dothemath = include('ronviews.php') / include('ronvids.php');
echo 'The answer is $dothemath'

?>

View Replies !
Date Math
I am trying to utilize the strtotime function but am getting unexpected results. I created the following function to return the difference between two dates. Code:

View Replies !
Math Question
i would like to generate a number between 2 and 7. i want my generating function to favor lower numbers and i want higher numbers to be less likely - the higher, the more remote ths possibility.

View Replies !
Math Errors In 5.2?
I upgraded my server over the weekend from php4.4 to php5.2. Since then, I've had a heck of a time getting math formulas to work correctly. Are there any known changes to how php5 handles arithmetic operations?

View Replies !
Math Function
I've finally managed to create a function that will add all the arguments passed to it using a variable number of arguments as seen below:

function addition()
{
$numargs = func_num_args();
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
$result += $arg_list[$i];
}
return $result;
}

addition(50, 10, 5); //outputs 65
addition(1, 2); //outputs 3

The problem I'm having is doing the same thing for subtracting the arguments from one another. I've tried changing it to "$result -= $arg_list[$i];" but I always end up with a negative number. Code:

View Replies !
AVG Calculation
how to times my AVG answer by 10, my AVG shows and answer of 5 and i want it to show 50 representing the percentage, Also how would i show this as a percentage not a normal number.

View Replies !
Calculation
I'm creating an order form whereby result is displayed in the next page.

<input type="radio" name="gift" value="toy A"/>
<input type="radio" name="gift" value="toy B"/>
<input type="radio" name="gift" value="toy C"/>

They cost $5, $10 and $15 respectively.

<input type="checkbox" name="extras[]" value="Ribbon" />
<input type="checkbox" name="extras[]" value="Card" />
<input type="checkbox" name="extras[]" value="Wrapper" />
<input type="checkbox" name="extras[]" value="Box" />

Additional $1 for each of the extras checked.

How can I know which toy is chosen, which extras are selected and total up the cost? I just need to display the final total cost.

View Replies !
Sql Calculation
I just started using php and mysql for the past 1 month and slowly picking things up: i got a question on DB calculation. Pls refer the table below:

account number ¦ payment
-----------------------------
45621 ¦ $100.00
45621 ¦ $50.00
42000 ¦ $70.00
42000 ¦ $20.00
42000 ¦ $30.00
45656 ¦ $95.00

For the account number, notice that there is duplicate account number with different payment value. Now, how do I calculate the total value for each account ie: for 45621 = $ 150.00 and 42000 = $120.00.

View Replies !
Multiplication Math Not Working ?
I had this in a function - so I've just moved this out of the function
for readabilty.

Can someone please tell me WHY does this not return the result of :
n1 times n2

As I said - I took it out of the function just for readability :
In my php file :

View Replies !
Strange Math Result... 1-1= -1 ???
See something weird on this result?

632.35 - 632.35 = -1.13686837722E-013

If anyone has seen something similar, please let me know!

this a part of the code:

$total_due = abs( $order_total ) - abs( $total_paid );
echo( $order_total . " - " . $total_paid . " = " . $total_due );

I've added the abs() in order to be sure that I'm dealing with
numbers, I've also used is_numeric() and confirmed it.
The echo() is showing the result I pasted up there.

View Replies !
MySQL Date Math
I have this query to sum timesheet entries in my application. The user can enter hours and/or minutes and then we calculate the value. My question is about whether I can change the value back to hours:minutes for printing. This is my query right now:

SELECT timesheet_entries.id, Sum(COALESCE(timesheet_entries.hours,0))  + Sum(COALESCE(timesheet_entries.minutes,0)/60) AS Rtotal FROM timesheet_entries  GROUP BY timesheet_entries.id

It takes the hours and the minutes and combines them to output a number like 3.5 for 3 hours 30 minutes. Is there a way to break that number back down and convert it to at least look like 3:30. Some users, in testing, are getting confused by the output of 3.5 after entering 3 hours and 30 minutes (which is annoying but I digress.)

View Replies !
Simple Math From Form
I'm trying to setup some simple math to happen after a form submission occurs.
Here a snippet of the current code MySQL submit code:

<?php
$con = mysql_connect("localhost", "xxx", "xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("db", $con); $sql="INSERT INTO ATS2007 (value1, value2, value3) VALUES ('$_POST[value1]','$_POST[value2]','$_POST[value3]'" ;if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());  }

Essentially what I want it to do is this - > (Value2 - Value1) and then store it at Value4. Is this something that I do in the form action code (as show above) or in the form itself?

View Replies !
Which Is Fast, For Math Purposes
Lets say (hypothethically) I have an array of 2000 numbers and I need to perform 1000 actions on these numbers.

and my server allows me to upload c/c++ scripts.  Would it be faster to use php to do the 2 million math operations or allow C/C++ to run it and use the return of that as the done function.  I'm not asking is C/C++ faster than php i'm asking is the bonus from going to C/C++ enough to compensate for the function exec().

View Replies !
Basic Math Functions
What's the codes for basic math functions, like +,  - , / , * , %

View Replies !
Date Math On 'mm/dd/yyyy'
I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier than today. How do I do that?

View Replies !
Math Being Used In UPDATE Query
Is it possible to have a "subtract 1" type command for credits in this mysql query...

UPDATE members SET CREDITS='$reduced' WHERE ID='$member'

I want to update the credits by subtracting 1 from the row's credit's value, I just want to use one line instead of having one line like this...

SELECT CREDITS FROM members WHERE ID='$member'

and then having php do credits minus one as the "$reduced" variable's value
and then running this query

UPDATE members SET CREDITS='$reduced' WHERE ID='$member'

There has to be a way to just update a row in the members table that will subtract 1 from the current value of credits in the row.

View Replies !
Powers (The Math Kind)
I am trying to make a simple program that will count the powers of a number. For instance, if you put in variable X = "5"; it would count up the powers of five.

View Replies !
Math Functions With Decimals
I'm trying to display a decimal number and i want it to be displayed with 2 decimal numbers always. e.g. 5.95. the problem with this is that when the number is 10.00 it will be displayed as 10. is there a way to fix this?

View Replies !
Primer On Math Functions
Can anyone point me to a tutorial or something dealing with math in mysql queries? I'm thinking along the lines of multiplying two fields of each result row together, or summing up one field in all rows.

I know there's got to be a better way than retrieving the result and looping through it in php. The mysql manual talks a bit about arithmetic and other functions, but all their examples seem to be on actual numbers, not result fields. Probably just looking in the wrong place though.

View Replies !
Date Math: Tutorial Out There
I'm looking for a tutorial on doing date math in PHP. Specifically, I need to be able to add or subtract days and be sure the month and year get changed where appropriate. Example:

2006-04-05 MINUS 7 days = 2006-03-31
2006-12-26 PLUS 7 days = 2007-01-01
etc.

I'm having difficulty coiming up with a simple way to do it without screwing up my table data which is stored as YYYY-MM-DD.

View Replies !
Math Functions In SQL Statement
I'm trying to create a SELECT order based on several database fields... SELECT * FROM myTable WHERE 1 ORDER BY field1 DESC, field2 DESC, field3 DESC The thing is, I don't want to order it preferentially by one, then the other, then another.

I'm not 100% familiar with the math functions in an SQL statement, but is there anyway I can sum the values of all fields into a single value, and then create an order off that value? ... I'm thinking something similar to CONCAT for aggregating text and field values, but mathematical.

I don't want a SUM of all values, but rather a SUM for the field values in individual rows... can't figure this one out.

View Replies !
Date Calculation In PHP
Hi!

I'm trying to get a date with PHP i.e. 14 days from now in yyyy-mm-dd or any other format like print (date("Y-m-d")) + 14 days.

Is there an easy way to do this??

View Replies !
Binary Calculation
I need some way of determining which bits are set in a binary number.

eg.

Decimal Number: 40
Binary Number: 00101000

How can I determine that the binary equivalent of 8 and 32 are set??

View Replies !
Duration Calculation
I had a look at a post here in regards to this issue but after it i was blank. I gave it a shot and i got a wrong value.

View Replies !
Time Calculation With Php
users of my web site enter time and distance of a run the did into a http-form. the data will be processed by a php-script and stored in an mysql-database. what i want to do is to calculate from the data what time they needed for a 1k-run -> i.e.: if one runs 12k in one hour it will be 5 minutes for 1k.

the problem is, the data from the textfield of the form comes in as a string and hence i just can't divide the data ride away.

time input should look like: 1:00:00
distance input should look like: 12
and output should look like: 0:05:00

any ideas?

View Replies !
Leave Calculation
for example, for the first 2 years, he does not has bonus leave, on the third year onwards, he has 1 day increase. by default, he has 18 days leave, on the third year, he can carry forward the 10 days from the previous year, now, he has 28 days leave plus 1 day bonus leave = 29 days. same with the fourth year, he can carry forward 10 days, so, he has 39 days leave plus 1 day bonus leave = 40 days.

if he apply 2 days leave in the first 2 years, the balance is 16 days. on the third year, he only can carry forward 10 days only, not 16 days. if he apply 12 days leave in the first 2 years, the balance is 6 days. on the third year, he only can carry forward 6 days only, not 10 days. this is the problem that u don't know how to fix, can you teach me?

View Replies !
Time Calculation
does anyone know how to calculate time difference between 2 periods of time(say 12:00 am and 1:00am) ?? i have been trying to come up with an algorithm but bugs keep crawling in.


View Replies !
Date Calculation
I would like to make a selection from the database in the following manner ie which is created in the last 1 week or last 2 weeks. There is one field named created_date in the database and the date is in the format yyyy-mm-dd. I have seen date calculations based on month and year but didn't see one on a week basis.

View Replies !
Days Calculation
i can't seem to get this what im trying to do is work out the elapsed time in DAYS from 2 dates code is:

             $time_now = time();
             $d2 = date('Y-m-d H:i:s', $time_now);
             $d1 = date('Y-m-d H:i:s', strtotime($ng_date));
             $days = ceil(($d1 - $d2) / (60 * 60 * 24));
when echoing out it produces:

2007-06-02 16:53:24
2007-08-27 18:00:51
which is right, but $days echoes out as 0.

View Replies !
Add Trailing Zero To Calculation
I am pulling a couple of numbers from a database (productcost and productprice).  I am trying to subtract the cost from the price to calculate the profit of the item.  I have this part working with no problem. My problem lies when the final number has a trailing zero it will drop that number (i.e. 1.2 as opposed to 1.20)

Here is what I am using:

$profit = $row_viewcoils['ProductPrice'] - $row_viewcoils['ProductCost'];
      echo $profit;

View Replies !
Calculation Form
I need to build a calculation form.. basically they click a couple radio buttons from features they want and then the user enter's in the number of words of the document and then they hit "calculate" button and it gives them an estimate for a quote. Need a little help with the structure of the form, anyone shed some light?

View Replies !
Percentage Calculation In Php
i have the following calculation code that adds the $total and the $tax variables..

number_format($total+$tax,2,'.','')

how i can calculate the total and then subtract 15% of the final value.

View Replies !

Copyright © 2005-08 www.BigResource.com, All rights reserved