473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Error: Divide by 0

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.
Feb 17 '07 #1
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
Feb 17 '07 #2
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
Feb 18 '07 #3
[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
Feb 18 '07 #4
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
Feb 18 '07 #5
ronverdonk
4,258 Expert 4TB
Thanks for showing us your solution to this.

Ronald :cool:
Feb 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
0
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...
1
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...
4
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>
0
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...
4
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 =...
1
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...
10
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...
13
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...
8
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.