FAST REPORT的非零的處理,在BEFOREPRINT事件

FAST REPORT的非零的處理,在BEFOREPRINT事件

 

CODE如下

沒有想到這麼麻煩

 

private void Text01_BeforePrint(object sender, EventArgs e)
{
    decimal moneys = 0;
    decimal earns = 0;

    // 嘗試取得總值,並確保不為 null
    object totalMoneys = Report.GetTotalValue("Total淨額");
    object totalEarns = Report.GetTotalValue("Total毛利");

    if (totalMoneys != null) 
        decimal.TryParse(totalMoneys.ToString(), out moneys);

    if (totalEarns != null) 
        decimal.TryParse(totalEarns.ToString(), out earns);

    // 檢查 moneys 是否為 0,避免除零錯誤
    if (moneys != 0)
    {  
        decimal PCTS = earns / moneys;
        Text27.Text = PCTS.ToString("P2"); // 顯示為百分比格式
    }
    else
    {
        Text27.Text = "0";
    }
}

 

自我LV~