简体   繁体   中英

C# syntax error -

I'm not quite sure why I'm getting an error in the 1 + annIntRate and numYears variables in my calcButton event handler. The error I'm getting for both variables is "Cannot convert from decimal double", but both should be decimals. Please help.

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");
            }


        }
    }
}

This might do the trick for you

decimal presentValue = (decimal) futureValue / (decimal) Math.Pow((double) (1 + annIntRate), (double) numYears);

Because Math.Pow Takes (Double, Double) which in returns, returns a specified number raised to the specified power.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM