开发者

Some basic error in double value calculation

开发者 https://www.devze.com 2023-04-13 06:23 出处:网络
I have a small question which is confusing me, I have a variable of type double named as RevisionRatio in which I am using this expression.

I have a small question which is confusing me, I have a variable of type double named as RevisionRatio in which I am using this expression.

RevisionRatio = ((WriterRevisions/TotalRevisions) * 100) now lets consider that WriterRevisions = 5 and TotalRevisions = 11 so it makes ((5/11)*100) = 45.45% but for some reason it is giving me 0 all the time, I know I am doing some small mistake and I can't seem to find it on google, your help is greatly appreciated, this is my code.

    protected void MyMonthlyRevisionRatio(bool DateSpecified)
{
    int TotalRevisions = 0;
    int WriterRevisions = 0;
    double RevisionRatio = 0;
    using (LogiConnection = new SqlConnection(CIPConnection))
    {
        LogiConnection.Open();
        DateTime DT = DateTime.Now;
        using (LogiCommand = new SqlCommand(Functions.NewString("SELECT COUNT(DISTINCT LCR.revision_id) AS revisions FROM LogiCpsRevisions AS LCR INNER JOIN LogiCpsLogs AS LCL ON LCL.project_id = LCR.project_id WHERE (DATENAME(mm, GETDATE()) = DATENAME(mm, LCL.creation_date))"), LogiConnection))
        {
            LogiCommand.Parameters.AddWithValue("@writer_id", HFSqlParameters.Value);
            TotalRevisions = Convert.ToInt16(Functions.GetResults(LogiCommand)[0]);
        }
        if (DateSpecified)
        {
            using (LogiCommand = new SqlCommand(Functions.NewString("SELECT COUNT(DISTINCT LCR.revision_id) AS revisions FROM LogiCpsRevisions AS LCR INNER JOIN LogiCpsLogs AS LCL ON LCL.project_id = LCR.project_id WHERE (LCL.writer_id = @writer_id) AND (DATENAME(mm, GETDATE()) = DATENAME(mm, LCL.creation_date)) AND (creation_date BETWEEN @FromDate AND @ToDate)"), LogiConnection))
            {
                LogiCommand.Parameters.AddWithValue("@writer_id", HFSqlParameters.Value);
                LogiCommand.Parameters.AddWithValue("@FromDate", FromDtPicker.SelectedDate);
                LogiCommand.Parameters.AddWithValue("@ToDate", ToDtPicker.SelectedDate);
                WriterRevisions = Convert.ToInt16(Functions.GetResults(LogiCommand)[0]);
                RevisionRatio = ((WriterRevisions / TotalRevisions) * 100);
                lblMyRevisionRatio.Text = "7. Revision ratio : " + RevisionRatio + "in the given period.";
            }
        }
        else
        {
            using (LogiCommand = new SqlCommand(Functions.NewString("SELECT COUNT(DISTINCT LCR.revision_id) AS revisions FROM LogiCpsRevisions AS LCR INNER JOIN LogiCpsLogs AS LCL ON LCL.project_id = LCR.project_id WHERE (LCL.writer_id = @writer_id) AND (DATENAME(mm, GETDATE()) = DATENAME(mm, LCL.creation_date))"), LogiConnection))
            {
                LogiCommand.Parameters.AddWithValue("@writer_id", HFSqlParameters.Value);
                WriterRevisions = Convert.ToInt16(Functions.GetResults(LogiCommand)[0]);
      开发者_如何转开发          RevisionRatio = ((WriterRevisions / TotalRevisions) * 100);
                //Response.Write(TotalRevisions);
                //Response.Write(WriterRevisions);
                //Response.Write(RevisionRatio.ToString());
                lblMyRevisionRatio.Text = "7. Revision ratio : " + RevisionRatio + " in the month of " + DT.ToString("MMMM") + ".";
            }
        }
    }
}

Thanks.


Change

int TotalRevisions = 0;     
int WriterRevisions = 0; 

to

double TotalRevisions = 0;     
double WriterRevisions = 0; 

integer division returns an integer - 0.4545 gets truncated and rounded down to 0


Let's say in a simple manner:

double x1 = 1 / 2; // x1 == 0
double x2 = 1.0 / 2; // x2 = 0.5

You are dividing integers and then upcasting the result to double. Cast one of the numbers to double.


Try this:

RevisionRatio = ((WriterRevisions * 1d / TotalRevisions) * 100);

That's because when you use (WriterRevisions / TotalRevisions) compiler use integers and so result is zero!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号