Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在if条件下应用==十进制值?_C#_Operators_C# 6.0 - Fatal编程技术网

C# 如何在if条件下应用==十进制值?

C# 如何在if条件下应用==十进制值?,c#,operators,c#-6.0,C#,Operators,C# 6.0,这是我一直试图执行的逻辑,但它带来了问题: protected void Price_Update_Click(object sender, EventArgs e) { decimal Price; var gvr = (GridViewRow)(sender as Control).Parent.Parent; int index = gvr.RowIndex; var box1 = (TextBox)GridView1.Rows[index].C

这是我一直试图执行的逻辑,但它带来了问题:

protected void Price_Update_Click(object sender, EventArgs e)
{
    decimal Price;   
    var gvr   = (GridViewRow)(sender as Control).Parent.Parent;
    int index = gvr.RowIndex;
    var box1  = (TextBox)GridView1.Rows[index].Cells[4].FindControl("TextBox1");
    bool prc  = decimal.TryParse(box1.Text, out Price);
    var PriceString = GridView1.Rows[index].Cells[4].Text.Replace(" AUD", "");

    var btn = (Button)sender;
    var row = (GridViewRow)btn.NamingContainer;
    var ProductNo = row.Cells[0].Text;
    var BranchNo  = row.Cells[6].Text;

    if (Price > 00.00)
    {
        var CS  = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Grocery_Demo; integrated security=SSPI";
        var con = new SqlConnection(CS);
        var cmd = new SqlCommand("UpdateProductQuantity", con);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        con.Open();
        cmd.Parameters.AddWithValue("@ProductPrice", Price);
        cmd.Parameters.AddWithValue("@ProductNo", ProductNo);
        cmd.Parameters.AddWithValue("@GroceryBranchNo", BranchNo);
        cmd.ExecuteNonQuery();
        con.Close();

        MessageBox("Price has been updated");
        DisplayProducts();
    }
    else if (Price == 00.00 || prc == false)
    {
        Label5.Text = "Please don't keep the price blank";
        DisplayProducts();
    }
}
我面临的问题是,在if条件下都有红线,它表示:

运算符“>”不能应用于“decimal”和“double”类型的操作数

我仍然不知道我错过的把戏在哪里


如果提供了适当的语法解决方案,这将很有帮助。

简而言之,由于无法使用
=
双精度
十进制
之间进行比较而导致的错误

Console.WriteLine((00.00).GetType()); // type is double
我们可以看到
00.00
的类型是
double
,但您的
Price
值是
decimal
类型,因此无法进行比较

您需要在数字末尾添加或
M
,因为
00.00
表示该值为
double
m
可以让它变成十进制

如果要将数字实数文字视为十进制,请使用后缀m或m


Try Price>0m m表示十进制文字,也表示Price==0m添加一个“m”,比如0.00m使其成为小数难以置信,我真的找不到
==
运算符的重复项。虽然在这种情况下,运算符的逻辑可以转移到任何比较运算符。但我认为这应该回答您的问题。如果我与您发布的错误消息相关
if (Price > 00.00m)