Crystal Reports: SafeDivide() Function

Divides the numerator by denominator, preventing a division by zero error.

Function SafeDivide(Numerator As Number, Denominator As Number) As Number
If Denominator<>0 Then SafeDivide=Numerator\Denominator
End Function

Parameters

numerator
The upper portion of the fraction.
denominator
The lower portion of the fraction.

Return Value

A numeric value or null.

Remarks

none

Example

SafeDivide({table.field0},{table.field1})

Requirements

This function requires the use of Basic Syntax.

2 thoughts on “Crystal Reports: SafeDivide() Function

  1. chuck arinze

    Was just looking for something else and i came across this. This will fail under certain conditions. To get it to work right do the following:

    Function SafeDivide(Numerator As Number, Denominator As Number) As Number
    If Denominator0 Then
    if Numerator0 then
    SafeDivide=Numerator\Denominator
    end if
    end if
    End Function

    OR (.NET only)

    Function SafeDivide(Numerator As Number, Denominator As Number) As Number
    If Denominator0 AndAlso Numerator0 Then
    SafeDivide=Numerator\Denominator
    end if
    End Function

  2. Pingback: Cogniza: Extreme Insight » Blog Archive » Crystal Reports: Strategy to Localize a Report (labels)

Leave a Reply

Your email address will not be published. Required fields are marked *