C#语法错误-
我不太清楚为什么calcButton事件处理程序中的1+annIntRate和numYears变量会出错。这两个变量的错误都是“不能从十进制双精度转换”,但两者都应该是小数。请帮忙C#语法错误-,c#,C#,我不太清楚为什么calcButton事件处理程序中的1+annIntRate和numYears变量会出错。这两个变量的错误都是“不能从十进制双精度转换”,但两者都应该是小数。请帮忙 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Present_Value
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Using the InputIsValid method to convert the users input and stores
// it in the arguments (passed by reference). If the conversion
// is successful, the method returns true. Otherwise it returns false.
private bool InputIsValid(ref decimal futureValue, ref decimal annIntRate, ref decimal numYears)
{
// Flag variable to indicate whether the input is good
bool inputGood = false;
// Try to convert all inputs to decimal.
if (decimal.TryParse(futureAmtTextBox.Text, out futureValue))
{
if (decimal.TryParse(yearsTextBox.Text, out numYears))
{
if (decimal.TryParse(rateTextBox.Text, out annIntRate))
{
//All inputs are good.
inputGood = true;
}
else
{
// Display an error message for anIntRate
MessageBox.Show("Annual Interest Rate is invalid.");
}
}
else
{
// Display an error message for the numYears
MessageBox.Show("Years in Savings is invalid.");
}
}
else
{
// Dislay an error message for future value
MessageBox.Show("Future amount is invalid");
}
// Return the reulst.
return inputGood;
}
private void calcButton_Click(object sender, EventArgs e)
{
// Variables for for the present, future value,
// annual interest rate, and number of years
decimal futureValue = 0m, annIntRate = 0m, numYears = 0m;
if (InputIsValid(ref futureValue, ref annIntRate, ref numYears))
{
// Calculate Present Value
decimal presentValue = (decimal)futureValue / (decimal)Math.Pow((**1 + annIntRate**), **numYears**);
presentValLabel.Text = presentValue.ToString("C");
}
}
}
}
这可能对你有用
decimal presentValue = (decimal) futureValue / (decimal) Math.Pow((double) (1 + annIntRate), (double) numYears);
因为需要(双倍), Double)在返回中,返回一个指定的数字,并提升到指定的幂次。
Math.Pow
需要Double
参数,而不是decimal
。另外,您在这里具体使用decimal做什么?为什么不改成双倍呢?我想我知道了。我把这一行改成了这一行,它起作用了://Calculate presentValue decimal presentValue=(decimal)futureValue/(decimal)Math.Pow((double)(1+Annentrate),(double)numYears);谢谢AlexD和un lucky…你是正确的。