locked
IF Values Zero Then Calculation Not Working RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my Gridview

    The below code gives wrong calucation if zero values then next row value is not adding.. why

    string[] splittedFileData = fileData.Split(new string[] { "'" }, StringSplitOptions.RemoveEmptyEntries);
                if (null != splittedFileData && splittedFileData.Length > 0)
                {
                    double total = 0;
                  
                    foreach (string data in splittedFileData)
                    {
                        double tempResult = 0;
                        if (double.TryParse(data.Trim(), out tempResult))
                            total += tempResult;
                    }
                    //-- display total in a template field column with lblTotal as Label control
                    (e.Row.FindControl("lblTotal") as System.Web.UI.WebControls.Label).Text = total.ToString();
    
                    //-- display no of data in a template field column with lblNoOfData as Label control
                    (e.Row.FindControl("lblNoOfData") as System.Web.UI.WebControls.Label).Text = splittedFileData.Length.ToString();
    
                    //-- display avarage in a template field column with lblAverageData as Label control
                    (e.Row.FindControl("lblAverageData") as System.Web.UI.WebControls.Label).Text = (total / splittedFileData.Length).ToString("##.##");
                }

     

    Sunday, March 26, 2017 7:44 AM

Answers

  • User-1509636757 posted

    gives wrong calucation if zero values then next row value is not adding.. why

    Because there is no single quote after 0 value. Add a single quote ' sign after 0 because you have written code to split by single quote '

    also, debug and see what values you are getting in splittedFileData string array. Please try debugging your code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 26, 2017 8:25 AM