Handling Zero Divisors and Zero Dividends

  • Last update:November 06, 2024
  • Overview

    Problem

    When designing reports that involve division operations, you may encounter scenarios where the divisor or dividend is zero. The following table shows the default calculation rules of FineReport in such scenarios.

    DividendDivisorQuotient

    0

    Non-zero

    0

    Non-zero

    0

    ∞ or -∞

    0

    0

    0

    These results are technically correct. However, in practical applications, you often need to handle cases where the divisor is zero or both the divisor and dividend are zero by displaying the quotients as can't calculate or avoiding quotients that are ∞ or -∞, as shown in the following figure.

    2024-11-06_14-25-23.png

    Solution

    • Use the IF formula to check whether both the divisor and dividend are zero. If so, display the quotient as can't calculate.

    • Change the quotients that are ∞ or -∞ under Condition Attribute > New Value.

    Example

    Data Preparation

    Create a general report and create a built-in dataset Embedded1. Add two integer-type fields Dividend and Divisor, and fill in some integer values, as shown in the following figure.

    2024-11-06_14-11-44.png

    Report Design

    Drag Dividend to cell A2 and Divisor to cell B2. Click Cell Element, and set Data Setting to List for cell A2. Insert the formula A2/B2 for Quotient in cell C2. The following figure shows the designed table.

    2024-11-06_14-16-47.png

    Preview the template. The quotients are calculated based on the default calculation rules of FineReport, as shown in the following figure.

    2024-11-06_14-25-23 copy.png

    Formula Setting

    Scenario Where Both the Dividend and Divisor Are Zero

    If both the dividend and divisor are zero, the quotient needs to be displayed as can't calculate. Otherwise, the division A2/B2 needs to be performed. In this case, you can modify the formula in cell C2 to IF(A2=0&&B2=0,"can't calculate",A2/B2). The following figure shows the results.

    2024-11-06_14-30-54.png

    Scenario Where the Divisor Is Zero

    If the divisor is zero, the quotient will be ∞ or -∞ normally. To avoid this, you can use the formula IF(B2=0,"0",A2/B2)to display the quotient as 0 when the divisor is zero and perform the division A2/B2 when the divisor is not zero. In this example, you can modify the formula in cell C2 to IF(A2=0&&B2=0,"can't calculate",IF(B2=0,"0",A2/B2)). The following figure shows the results.

    2024-11-06_14-54-41.png

    Setting New Value in Condition Attribute

    If the quotient is ∞ or -∞, the corresponding actual value is Infinity or -Infinity. You can change the cell value under Condition Attribute > New Value.

    iconNote:
    The word Infinity must start with an uppercase I.

    Select cell C2 and click Condition Attribute on the right attribute panel. To add a condition attribute, select New Value from the drop-down box of Attribute, and set New Value to string 0 (or values of other types as needed). Add two common conditions connected by OR, as shown in the following figure. When the cell value is Infinity (∞) or -Infinity (-∞), the value will be changed to 0.

    2024-11-06_14-38-40.png

    You can set the cell values in batches using Condition Attribute. If the values of multiple cells in the template are calculated as infinity/-infinity, you can select all these cells, right-click the mouse, select Condition Attribute from the drop-down box, and set the cell values in one step. This eliminates the need for repetitive IF formulas and significantly improves development efficiency.

    Effect Display

    PC

    Save the template. You can view the preview effect in the section "Problem."

    Mobile Terminal

    The preview effects on the DataAnalyst app and the HTML5 terminal are the same, as shown in the following figure.

    IMG_0163.jpg

    Template Download

    You can download the template: Handling Zero Divisors and Zero Dividends.cpt.


    Attachment List


    Theme: Report Application
    Already the First
    Already the Last
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy