有没有一种方法可以将转换为浮点数的字符串与实际浮点值与 if 语句进行比较?

Is there a way to compare a String converted into a float with an actual float value into an if-statement?

验证按钮时,我将字符串值转换为浮点值,但当我尝试将转换后的值与实际浮点值进行比较时,出现编译错误。我想在此 if 中创建一个比较,以不允许在应用程序中写入小于 50 的值。

private void tbBid_Validating(object sender, CancelEventArgs e)
        {
            var amount = 12345678.0f;
            tbBid.Text = amount.ToString();
            if(amount.ToString()<50)
            {
                e.Cancel = true;

                epbid.SetError(tbBid,">50 lei");
            }

        }

试试这个

//Reading value from text box
var amount = tbBid.Text;
//Parsing to float
float amountFloat = float.Parse(amount);

//Comparison
if (amountFloat < 50.0f)
{
      // Do your cancellation stuff
}

我做了一个简化版本,可能会对您有所帮助。我使用了您提供的代码和一个版本,其中 amount 首先是一个字符串值:

    // current example simplified
    float amount = 12345678.0f;
    string text = amount.ToString();
    if(amount < 50)
    {
        Console.WriteLine("Congratulations the first comparison worked!");
    }

    //if amount was a string to start with
    string amountText = "12345678.0";       
    float amountFloat;
    float.TryParse(amountText, out amountFloat);
    if(amountFloat < 50)
    {
        Console.WriteLine("Congratulations the second comparison worked!");
    }

这是 .Net fiddle:https://dotnetfiddle.net/utyWUc