双值计算中的一些基本错误

时间:2011-10-17 08:06:00

标签: c# types double

我有一个令我困惑的小问题,我有一个double类型的变量,名为RevisionRatio,我在其中使用此表达式。

RevisionRatio =((WriterRevisions / TotalRevisions)* 100)现在让我们考虑WriterRevisions = 5和TotalRevisions = 11所以它使得((5/11)* 100)= 45.45%但是由于某种原因它给了我0全部时间,我知道我正在做一些小错误,我似乎无法在谷歌上找到它,非常感谢你的帮助,这是我的代码。

    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") + ".";
            }
        }
    }
}

感谢。

3 个答案:

答案 0 :(得分:1)

试试这个:

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

那是因为当你使用(WriterRevisions / TotalRevisions)编译器时使用整数,结果为零!!

答案 1 :(得分:1)

让我们以一种简单的方式说:

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

您正在对整数进行除法,然后将结果向上转换为double。将其中一个数字投放到double

答案 2 :(得分:1)

更改

int TotalRevisions = 0;     
int WriterRevisions = 0; 

double TotalRevisions = 0;     
double WriterRevisions = 0; 

整数除法返回一个整数 - 0.4545被截断并向下舍入为0