C# 无法让运算符使用小数和双精度运算,但我需要它。有没有办法在不重做整个代码的情况下解决这个问题?
你好!!我在试图让我的代码正确执行总计时遇到问题。我发现错误运算符“+”不能应用于第行的“double”和“decimal”类型的操作数:C# 无法让运算符使用小数和双精度运算,但我需要它。有没有办法在不重做整个代码的情况下解决这个问题?,c#,C#,你好!!我在试图让我的代码正确执行总计时遇到问题。我发现错误运算符“+”不能应用于第行的“double”和“decimal”类型的操作数: namespace JoesAutomotive { public partial class frmAutomotive : Form { public frmAutomotive() { InitializeComponent(); } // i want to take each group box,
namespace JoesAutomotive
{
public partial class frmAutomotive : Form
{
public frmAutomotive()
{
InitializeComponent();
}
// i want to take each group box, and make them a reference.
private double OilandLubeCosts()
{
double OilLubeTotal = 0; //default value for the total in the groupbox.
{
if (chkOilChange.Checked) // if user wants an Oil Change for $28
{
OilLubeTotal += 26.00;
lblServiceandLabor.Text = OilLubeTotal.ToString("C");
}
if (chkLubeJob.Checked) // if user wants Lube Job for $18.00 // if user wants both, then it simply adds onto totals.
{
OilLubeTotal += 18.00;
lblServiceandLabor.Text = OilLubeTotal.ToString("C");
return OilLubeTotal; // return the checked amounts by adding and sending to OilandLubeCharges.
}
else
{
return OilLubeTotal; // return values even if none are checked.
}
}
}
private double FlushCosts()
{
double FlushTotal = 0;
{
if (chkRadiator.Checked)
{
FlushTotal += 30.00;
lblServiceandLabor.Text = FlushTotal.ToString("C");
}
if (chkTransmission.Checked)
{
FlushTotal += 80.00;
lblServiceandLabor.Text = FlushTotal.ToString("C");
return FlushTotal;
}
else
{
return FlushTotal;
}
}
}
private double MiscCosts()
{
double MiscTotal = 0;
{
if (chkInspection.Checked)
{
MiscTotal += 15.00;
lblServiceandLabor.Text = MiscTotal.ToString("C");
}
if (chkReplaceMuffler.Checked)
{
MiscTotal += 100.00;
lblServiceandLabor.Text = MiscTotal.ToString("C");
}
if (chkTireRotation.Checked)
{
MiscTotal += 20.00;
lblServiceandLabor.Text = MiscTotal.ToString("C");
return MiscTotal;
}
else
{
return MiscTotal;
}
}
}
private double PartsandLaborCharges()
{
double OtherCharges = 0;
{
if (double.TryParse(txtLabor.Text, out double LaborCost))
{
lblServiceandLabor.Text = LaborCost.ToString("C");
// exception handling! If the user types in the labor textbox and receives an error out of Labor Costs, throw error.
OtherCharges = LaborCost;
}
}
if (double.TryParse(txtParts.Text, out double PartsCost))
{
lblServiceandLabor.Text = PartsCost.ToString("C");
OtherCharges = PartsCost; // we want our output to be based on PartsCost as well.
return OtherCharges; // we take everything we added to our bill and sent the numbers back to our Other Charges.
}
else
{
return OtherCharges;
}
}
private decimal Taxes()
{
decimal PlusTaxes;
decimal PartsBought = Convert.ToDecimal(txtParts.Text);
PlusTaxes = PartsBought * 0.06m;
lblTaxonParts.Text = PlusTaxes.ToString("C");
// Have to use decimal because we cannot convert. m = money. So cents.
return PlusTaxes; // take the added price and sent back to reference.
}
private decimal TotalAmountCharged()
{
decimal TotalServiceFee;
TotalServiceFee = OilandLubeCosts() + MiscCosts() + FlushCosts() + PartsandLaborCharges() + Taxes();
//we take everything added up and apply to Total for complete pricing.
lblTotalFees.Text = TotalServiceFee.ToString("C");
return TotalServiceFee;
}
private void BtnCalculate_Click(object sender, EventArgs e)
{
OilandLubeCosts();
FlushCosts();
MiscCosts();
PartsandLaborCharges();
Taxes(); // we want to take what we called up there, get totals, and display when we press calculate.
}
}
}
看看我的代码,我必须把所有东西都转换成Int?我试着弄乱我的代码,把所有的代码都改成十进制,或者把所有的代码都改成双精度,但是我得到了很多错误。有没有办法解决这个问题而不必重做整个表单?谢谢大家! 去掉double,用decimal替换,它更适合您的任务
TotalServiceFee = OilandLubeCosts() + MiscCosts() + FlushCosts() +
PartsandLaborCharges() + Taxes();
您的Taxes函数返回十进制数,其中其他值为double,而您的TotalServiceFee假定为double。我认为您的parts和LaborCharges方法中也可能存在错误。如果有零件成本,它将替换人工成本。@Kajal我已经修复了这两个成本,仍然会得到错误代码……去掉double,用decimal替换,它更适合您的任务。
private decimal OilandLubeCosts()
{
decimal OilLubeTotal = 0; //default value for the total in the groupbox.
{
if (chkOilChange.Checked) // if user wants an Oil Change for $28
{
OilLubeTotal += 26.00m;
lblServiceandLabor.Text = OilLubeTotal.ToString("C");
}
if (chkLubeJob.Checked) // if user wants Lube Job for $18.00 // if user wants both, then it simply adds onto totals.
{
OilLubeTotal += 18.00m;
lblServiceandLabor.Text = OilLubeTotal.ToString("C");
return OilLubeTotal; // return the checked amounts by adding and sending to OilandLubeCharges.
}
else
{
return OilLubeTotal; // return values even if none are checked.
}
}
}
private decimal FlushCosts()
{
decimal FlushTotal = 0;
{
if (chkRadiator.Checked)
{
FlushTotal += 30.00m;
lblServiceandLabor.Text = FlushTotal.ToString("C");
}
if (chkTransmission.Checked)
{
FlushTotal += 80.00m;
lblServiceandLabor.Text = FlushTotal.ToString("C");
return FlushTotal;
}
else
{
return FlushTotal;
}
}
}
private decimal MiscCosts()
{
decimal MiscTotal = 0;
{
if (chkInspection.Checked)
{
MiscTotal += 15.00m;
lblServiceandLabor.Text = MiscTotal.ToString("C");
}
if (chkReplaceMuffler.Checked)
{
MiscTotal += 100.00m;
lblServiceandLabor.Text = MiscTotal.ToString("C");
}
if (chkTireRotation.Checked)
{
MiscTotal += 20.00m;
lblServiceandLabor.Text = MiscTotal.ToString("C");
return MiscTotal;
}
else
{
return MiscTotal;
}
}
}
private decimal PartsandLaborCharges()
{
decimal OtherCharges = 0;
{
if (decimal.TryParse(txtLabor.Text, out decimal LaborCost))
{
lblServiceandLabor.Text = LaborCost.ToString("C");
// exception handling! If the user types in the labor textbox and receives an error out of Labor Costs, throw error.
OtherCharges = LaborCost;
}
}
if (decimal.TryParse(txtParts.Text, out decimal PartsCost))
{
lblServiceandLabor.Text = PartsCost.ToString("C");
OtherCharges = PartsCost; // we want our output to be based on PartsCost as well.
return OtherCharges; // we take everything we added to our bill and sent the numbers back to our Other Charges.
}
else
{
return OtherCharges;
}
}
private decimal Taxes()
{
decimal PlusTaxes;
decimal PartsBought = Convert.ToDecimal(txtParts.Text);
PlusTaxes = PartsBought * 0.06m;
lblTaxonParts.Text = PlusTaxes.ToString("C");
// Have to use decimal because we cannot convert. m = money. So cents.
return PlusTaxes; // take the added price and sent back to reference.
}
private decimal TotalAmountCharged()
{
decimal TotalServiceFee;
TotalServiceFee = OilandLubeCosts() + MiscCosts() + FlushCosts() + PartsandLaborCharges() + Taxes();
//we take everything added up and apply to Total for complete pricing.
lblTotalFees.Text = TotalServiceFee.ToString("C");
return TotalServiceFee;
}
private void BtnCalculate_Click(object sender, EventArgs e)
{
OilandLubeCosts();
FlushCosts();
MiscCosts();
PartsandLaborCharges();
Taxes(); // we want to take what we called up there, get totals, and display when we press calculate.
}