Question : Expression to Handle the ZERO


Expression:
=IIF((((Fields!AMOUNT_VARIANCE_CENTRICA.Value)/(Fields!LCH_AMOUNT.Value))*100) <> 0 , (Fields!AMOUNT_VARIANCE_CENTRICA.Value & " : " & format ((((Fields!AMOUNT_VARIANCE_CENTRICA.Value)/(Fields!LCH_AMOUNT.Value))*100),"##,###,###0.00") & " % "),0)

Explanation of the Expression :
Formula Used :  (Fields!AMOUNT_VARIANCE_CENTRICA.Value)/(Fields!LCH_AMOUNT.Value)*100)

Here, In my  Stored Procedure ,
 Fields!AMOUNT_VARIANCE_CENTRICA.Value = 0
Fields!LCH_AMOUNT.Value =0
so 0/0 * 100 , is throwing the error.
I am getting #Error in the expression column in th report. If both the Values in the Numerator and the Denominator are euqal to zero , then the expression should display  zero otherwise the value derived from the formula..
The expression mentioned above is unable to handle the above mentioned situation , that's why it is throwing the error..  So Please provide me with the solution when both the columns are zero....
I will  appreciate your help...


Regards,

Sreekanth.

Answer : Expression to Handle the ZERO

Sreekanth,

I noticed that you're testing if AMOUNT_VARIANCE_CENTRICA is zero, but that may still cause "Division by zero".  To be 100% sure I recommend that you test LCH_AMOUNT on zero, because that's the denominator...

See snippet, that function will return the result of the division, or zero if LCH_AMOUNT is zero.  It also divides by 100 for the percentage.

You just need to use it like in your initial mail:
=Code.VarAmountVariance(Fields!AMOUNT_VARIANCE_CENTRICA.Value,Fields!LCH_AMOUNT.Value)

For the formatting, I recommend to use the Format property of the textbox in which you are putting this expression.  You can use something like the following for a percentage with 2 decimal positions: #,0.00 %

Hope this helps you out?

Valentino.
1:
2:
3:
4:
5:
6:
7:
Public Shared Function VarAmountVariance(ByVal AMOUNT_VARIANCE_CENTRICA As Decimal, ByVal LCH_AMOUNT As Decimal) As Decimal
  If LCH_AMOUNT = 0 Then
    Return 0
  End If
  
  Return  AMOUNT_VARIANCE_CENTRICA / LCH_AMOUNT  / 100
End Function
Random Solutions  
 
programming4us programming4us