Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Division by zero 3

Status
Not open for further replies.

Yazster

Programmer
Sep 20, 2000
175
CA
I have a formula which divides two numbers and returns the result as a percentage.

For Example: X/Y

If x = 0 and y = 3, then I want it to be 0%

However, if X=3 and Y=0, then I don't want a 0 value, I want a blank (DIV BY 0)

When I try to insert this in my formula, I get a "a number is required here" error.

My Formula

if y>0 then
x/y
else
"N/A"


Any Ideas?
 
It's because you are mixing the return types of the function. A function must always return the same type - text or numeric. your function could potentially return a text or a number depending on the input value.

Taking your example, do the following :

if y>0 then
ToText( x/y )
else
"N/A"


This converts the numeric calculation to text, so the return type of the function will always be Text.

Good Luck.


Chris Lawton
Chris.Lawton@GoldMine.com

 
Your posting isn't clear whether you want a blank or a message when div by zero is attempted. If you want a blank,
an alterntive is to conditionally suppress the formula:
//conditional suppression
y=0
//formula
if y<>0 then
x/y

That will give a blank if division by zero is attempted. Leaving the formula without an else statement is allowed, although it should not be usual practice. Malcolm
 
Or you could have two fields.

Your original that returns a zero (formatted to suppress if 0)
Then supreimpose another formula that says:

If {@percentfield} = 0 then &quot;NA&quot; else &quot;&quot;

If formatted correctly, only one will print.
This leaves the original as a numeric which might be easier to align and format. Ken Hamady
Crystal Reports Training and a
Quick Reference Guide to VB/Crystal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top