I have a percentage calculation, but if no data is available, I get the following error message:
Division by zero
calculation
$ratio = ($postup*100)/($postup + $postdown);
Devision by zero is undefined.
If both $postup
and $postdown
are null
(not set) you will get a division by zero i.e. null + null == 0
.
Furthermore the same problem will occur if $postup * -1 == $postdown
.
Since a division by zero is undefined you will need to add a fallback for this.
What this fallback would be is application specific but would look something like
$ratio = null;
if($postup + $postdown == 0) {
$ratio = xxx;
} else {
$ratio = ($postup*100)/($postup + $postdown);
}
Please also be aware that $postup * 100
will be equal to 0
if $postup == null