We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to correct div/0 in class CalcQuickBase

formula a/b + c/d, when b or d 0 method ParseAndCompute return infinity.
How replace div/0 -> 0?

8 Replies

KB Kanimozhi Bharathi Syncfusion Team December 1, 2016 09:31 AM UTC

Hi Alex,   
   
While calculating division on zero, CalcEngine returns “#Div /0!” error which is the same result as in MS Excel.  Your requirement can be achieved by using IF condition like below code example   
   
   
CalcQuickBase calc = new CalcQuickBase();   
calc["a"] = "5";   
calc["b"] = "0";   
calc["c"] = "4";   
calc["d"] = "2";   
var result = calc.ParseAndCompute("if([b] = 0, 0 , [a]/[b])+ if([d] = 0, 0 , [c]/[d])");   
   
    
Regards    
Kanimozhi B    



AL alex December 1, 2016 09:46 AM UTC

If there is a complicated formula. It is necessary to add much function "if". Perhaps there is another way of “#Div /0!” error handling?


KB Kanimozhi Bharathi Syncfusion Team December 1, 2016 12:23 PM UTC

Hi Alex, 
 
You can achieve your  requirement by defining the custom formula  and adding in CalcEngine LibraryFunction like below code example 
 
 
CalcQuickBase calc = new CalcQuickBase(); 
calc.Engine.AddFunction("Div", new Syncfusion.Calculate.CalcEngine.LibraryFunction(ComputeDivision)); 
var result1= calc.ParseAndCompute("Div(5, 0)"); 
 
//Implementation of function 
public string ComputeDivision(string range) 
{      
    var args = range.Split(new char[]{CalcEngine.ParseArgumentSeparator}, StringSplitOptions.None); 
    if (args[1] == "0") 
        return "0"; 
    var parse = Int32.Parse(args[0])/Int32.Parse(args[1]); 
    return parse.ToString(); 
} 
 
We have also prepared the sample for your reference. Please find the sample link below 
 
 
Regards 
Kanimozhi B 



AL alex December 1, 2016 02:07 PM UTC

Thanks, I'll try this solution


AL alex December 1, 2016 03:06 PM UTC

with simple formula work, with formula like (a/b+c/d)/([k]/[l]+[j]) not work external div


KB Kanimozhi Bharathi Syncfusion Team December 2, 2016 11:16 AM UTC

Hi Alex,  
  
We have modified the sample based on your requirement. Please find the below code example for your reference, 
  
  
CalcQuickBase calc = new CalcQuickBase();  
calc.Engine.AddFunction("Div", new Syncfusion.Calculate.CalcEngine.LibraryFunction(ComputeDivision));  
var result1= calc.ParseAndCompute("DIV((DIV(5,0)+ DIV(9,5)),(DIV(4,0) + 6))"); // (a/b + c/d)/(k/l + j]) 
  
//Implementation of function  
public string ComputeDivision(string range)  
{       
    var args = range.Split(new char[]{CalcEngine.ParseArgumentSeparator}, StringSplitOptions.None);  
    string s1 = calc.Engine.GetValueFromArg(args[0]); 
    string s2 = calc.Engine.GetValueFromArg(args[1]); 
    if (s2 == "0")  
        return "0";  
    var parse = Int32.Parse(s1)/Int32.Parse(s2);  
    return parse.ToString();  
}  
  
We have also prepared a sample for your reference and the link for the same has been given below  
 
 
Regards  
Kanimozhi B  



AL alex December 2, 2016 02:09 PM UTC

Thanks, it helped.


DB Dinesh Babu Yadav Syncfusion Team December 5, 2016 08:57 AM UTC

Hi Alex,
 
Thanks for the update.
 
Regards,
​​​​​Dinesh BabuYadav


Loader.
Up arrow icon