C# 如何在c语言中验证价格范围值#
我正在为我的销售和库存添加材料。到目前为止,我需要一个验证,当添加材料时,销售价格必须大于采购价格 这是我的代码,我在if/else语句中得到一个错误,其中我的txtPurchasePrice和txtSellingPrice的值是十进制的(18,2)C# 如何在c语言中验证价格范围值#,c#,asp.net,C#,Asp.net,我正在为我的销售和库存添加材料。到目前为止,我需要一个验证,当添加材料时,销售价格必须大于采购价格 这是我的代码,我在if/else语句中得到一个错误,其中我的txtPurchasePrice和txtSellingPrice的值是十进制的(18,2) 我的错误 CS0019:运算符“>=”不能应用于“字符串”类型的操作数 和“字符串” 为什么=或使用Convert.ToDecimal(txtSellingPrice.Text)>=Convert.ToDecimal(txtPurchasePr
我的错误 CS0019:运算符“>=”不能应用于“字符串”类型的操作数 和“字符串” 为什么
=
或使用Convert.ToDecimal(txtSellingPrice.Text)>=Convert.ToDecimal(txtPurchasePrice.Text)
不能像这样比较字符串值使用Convert.ToDecimal(txtSellingPrice.Text)>=Convert.ToDecimal(txtPurchasePrice.Text)
字符串值不能像那样进行比较一个字符串可以大于另一个字符串吗
在进行比较之前,需要将字符串转换为数值
因为你是在处理钱的问题,所以你应该使用Decimal
:
if (Decimal.Parse(txtSellingPrice.Text) >= Decimal.Parse(txtPurchasePrice.Text))
var sellingPrice = decimal.Parse(txtSellingPrice.Text);
var purchasePrice = decimal.Parse(txtPurchasePrice.Text);
if (sellingPrice >= purchasePrice)
{
// stuff
}
一个字符串可以大于另一个字符串吗
在进行比较之前,需要将字符串转换为数值
因为你是在处理钱的问题,所以你应该使用Decimal
:
if (Decimal.Parse(txtSellingPrice.Text) >= Decimal.Parse(txtPurchasePrice.Text))
var sellingPrice = decimal.Parse(txtSellingPrice.Text);
var purchasePrice = decimal.Parse(txtPurchasePrice.Text);
if (sellingPrice >= purchasePrice)
{
// stuff
}
我假设txtSellingPrice
和txtPurchasePrice
是TextBox
控件,因此它们的Text
属性的类型为string
。您不能使用>=
,因为它没有语义意义。您需要首先将文本解析为decimal
:
if (Decimal.Parse(txtSellingPrice.Text) >= Decimal.Parse(txtPurchasePrice.Text))
var sellingPrice = decimal.Parse(txtSellingPrice.Text);
var purchasePrice = decimal.Parse(txtPurchasePrice.Text);
if (sellingPrice >= purchasePrice)
{
// stuff
}
如果不确定文本是否为有效的decimal
值,请使用decimal.TryParse
:
decimal sellingPrice;
if (!decimal.TryParse(txtSellingPrice.Text, out sellingPrice))
{
// Not a valid decimal, do something.
}
decimal purchasePrice;
if (!decimal.TryParse(txtPurchasePrice.Text, out purchasePrice))
{
// Not a valid decimal, do something.
}
if (sellingPrice >= purchasePrice)
{
// stuff
}
我假设txtSellingPrice
和txtPurchasePrice
是TextBox
控件,因此它们的Text
属性的类型为string
。您不能使用>=
,因为它没有语义意义。您需要首先将文本解析为decimal
:
if (Decimal.Parse(txtSellingPrice.Text) >= Decimal.Parse(txtPurchasePrice.Text))
var sellingPrice = decimal.Parse(txtSellingPrice.Text);
var purchasePrice = decimal.Parse(txtPurchasePrice.Text);
if (sellingPrice >= purchasePrice)
{
// stuff
}
如果不确定文本是否为有效的decimal
值,请使用decimal.TryParse
:
decimal sellingPrice;
if (!decimal.TryParse(txtSellingPrice.Text, out sellingPrice))
{
// Not a valid decimal, do something.
}
decimal purchasePrice;
if (!decimal.TryParse(txtPurchasePrice.Text, out purchasePrice))
{
// Not a valid decimal, do something.
}
if (sellingPrice >= purchasePrice)
{
// stuff
}
要比较两个字符串的数值,必须将它们解析为数字类型,例如double 要比较两个字符串的数值,必须将它们解析为数字类型,例如double 因为这些运算符用于数值或枚举值(除了| |
)。你怎么认为一根弦可以和另一根弦“更大或相等”?这个“更大”的主题在文本比较中会很复杂。我只是想跟操作员说清楚:)谢谢!因为这些运算符用于数值或枚举值(除了| |
)。你怎么认为一根弦可以和另一根弦“更大或相等”?这个“更大”的主题在文本比较中会很复杂。我只是想跟操作员说清楚:)谢谢@朱尔斯:什么整数非常感谢:)你真的回答了我的问题,它也起了作用@朱尔斯:什么整数非常感谢:)你真的回答了我的问题,它也起了作用!