Ok so I am working on a page: http://imageparticle.com/apache/Draft%201/newthesis.php
This is really my first time using php, but anyway, basically the thing takes the input from the first page, and then shows the % change from year to year on the next page for each category (cash, etc)
the PROBLEM right now is that when it does this, and the user hasnt filled in every single input, it gives div by 0 errors.
I know perfectly well why this happens, however i'm not quite sure how to fix it? Ideally i would just like to set all of the default values to 1 instead of 0.... as this would eliminate the problem, however, i'm not sure how to do that? Any help would be appreciated. Thanks.
5 1663
Actually after thinking about it, the default value doesnt matter, people need to be able to enter 0's.
So for example if for 3 years, Accounts Receivable were 200, 0, 0 respectively... then the % change would be -100%, and 0%.... but without the error div by 0...?
Also, if it were $0, $0, $200... %'s would need to be 0% and 100%.
Thank you in advance
Atli 5,058
Expert 4TB
Hi.
I have no clue what your calculating there but you could fix it with a simple if statment, that checks if the field is 0 before you calculate.
Like this.
[PHP]
$var = 0;
if(abs($var) != 0)
{
// Do your calculations
}
else
{
// Do some other calculations
}
[/PHP]
It is also possible to catch an exception in PHP 5, similar to what many other languages offer. But that is much more complex.
Click here to see the manual at php.net
[PHP]
function change($x,$y) //This function takes the input of 2 numbers and calculates the % change
{
$changeout= (($y - $x) / $x) * 100;
if ($changeout>0)
echo "<FONT color='#41A317'><B>" . number_format($changeout,2) . "%" . "</B></FONT>";
elseif ($changeout==0)
echo "<B>" . number_format($changeout,2) . "%" . "</B>";
else
echo "<FONT color='#FF0000'><B>(" . -number_format($changeout,2) . "%)" . "</B></FONT>";
}
?>[/PHP]
That is the code i'm talking about... the problem happens when $changeout= (($y - $x) / $x) * 100; is calculated and $x is = 0. Is there a way to write the function so that if x==0 it just returns 0%? I tried to make it do that, however it was screwing up the current IF loop
woohoo. Ok well i sorta took your advice, it inspired me anyway. I split it into two functions. The first checks for 0's and handles accordingly, the second changes the color.
[PHP]<?php
function change($x,$y) //This function takes the input of 2 numbers and calculates the % change
{
if (abs($x)!=0)
{
$changeout= (($y - $x) / $x) * 100;
changecolor($changeout);
}
else
{
echo "<B>" . "-" . "</B>";
}
}
function changecolor($col) //changes the color of the output
{
if ($col>0)
echo "<FONT color='#41A317'><B>" . number_format($col,2) . "%" . "</B></FONT>";
elseif ($col==0)
echo "<B>" . number_format($col,2) . "%" . "</B>";
else
echo "<FONT color='#FF0000'><B>(" . -number_format($col,2) . "%)" . "</B></FONT>";
}
?>[/PHP]
that is the code in case anyone was interested
Thanks for showing us your solution to this.
Ronald :cool:
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ximo |
last post by:
Hello, I'm programing an advanced calculator, and I have many problems with
the execution errors, specually with the division by 0.
And my question is how can show the execution error whitout...
|
by: Ximo |
last post by:
>> Ximo> My question is how can show the execution error whitout exit of
>> the program, showing it in the error output as
>> Skip> You need to catch ZeroDivisionError. Here's a trivial...
|
by: Donald Canton |
last post by:
Hi,
I'm using Bjarne's book to learn C++ and am stuck on the Calc program
in Section 6. Everything works fine except when I try to use
istringstream to parse a token from the command line. I...
|
by: learning_C++ |
last post by:
Hi,
I try to use
input = new istrstream(argv,strlen(argv));
in my code, but it always says error: " error: parse error before `('
token"
please help me!
Thanks,
#include <map>
|
by: Gary Carson |
last post by:
Can anyone tell why the query below would throw a divide-by-zero
error? The only reason I can see for the error happening would be if
SUM() came out to be zero, but this never happens with the...
|
by: Sheldon |
last post by:
Hi,
I have a written a script that will check to see if the divisor is zero
before executing but python will not allow this:
if statistic_array > 0.0:
statistic_array =...
|
by: John |
last post by:
The following code has run-time error on "divide by 0" error. I expect
"do the work<br>" will not print in the browser, since it happen after
the code
that cause the error. However, this is the...
|
by: Lisa |
last post by:
In translating the formula for calculating lottery odds for various
conditions into a Visual Basic Program, I have apparently missed something
in that I get errors in the part of the calculation...
|
by: mike3 |
last post by:
Hi.
(crossposted because the program is in C++ and some C++-related
elements are discussed, hence comp.lang.c++, plus general program
design questions are asked, hence comp.programming.)
I'm...
|
by: Kid Programmer |
last post by:
Hello guys. I am trying to create a calculator program in a GUI window. When I compile the code I get the following errors:
Severity and Description Path Resource Location Creation Time Id
The...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |