Calculadora Interés Compuesto con C#

 



Aprende a crear una calculadora para determinar las ganancias de tus inversiones en CETES-GBM-SuperTasas etc. En C#

Código


using System.Linq.Expressions;


namespace CalcularInteresCompuesto

{

    public partial class Form1 : Form

    {


        double montodepositoInicial;

        double montodepositoInicialLabel;

        double porcentajeInteres;

        int years;

        string frecuenciaAnualInteres;

        double aportacionesAdicionales;



        public Form1()

        {

            InitializeComponent();


        }



        private void btnCalcular_Click(object sender, EventArgs e)

        {


            calcularImpuesto();

        }



        public void calcularImpuesto() {


            try

            {

                montodepositoInicial = Convert.ToDouble(txtDepositoInicial.Text);

                porcentajeInteres = Convert.ToDouble(txtTasaIntresAnual.Text);

                years = Convert.ToInt16(txtYearsInvertir.Text);

                frecuenciaAnualInteres = cmbFrecuencia.Text;

                //  aportacionesAdicionales = Convert.ToDouble(txtAportacionesAdicionales.Text);

            }

            catch(Exception e) { 

            

                      

            }


            //Calculo de interes compuesto


            switch (frecuenciaAnualInteres)

            {

                case "Anualmente":

                    // code block

                    calcularInteres(1, years);


                    break;

                case "Mensualmente":

                    // code block

                    calcularInteres(12, years);


                    break;


                case "Quincenalmente":

                    // code block

                    calcularInteres(24, years);


                    break;


                case "Semanalmente":

                    // code block

                    calcularInteres(52, years);


                    break;


                case "Diariamente":

                    // code block

                    calcularInteres(365, years);


                    break;


                default:

                    // code block


                    break;

            }

        }



        private void calcularInteres(int dias, int year)

        {


            try

            {


                int diasInverion = dias * year;


                montodepositoInicial = Convert.ToDouble(txtDepositoInicial.Text);

                montodepositoInicialLabel = Convert.ToDouble(txtDepositoInicial.Text);

                porcentajeInteres = Convert.ToDouble(txtTasaIntresAnual.Text);

                porcentajeInteres = porcentajeInteres / dias;

                frecuenciaAnualInteres = cmbFrecuencia.Text;

                aportacionesAdicionales = Convert.ToDouble(txtAportacionesAdicionales.Text);


                double interesMensual = 0;

                double interesMensualAcumulado = 0;

                double totalAnual;

                double promedioInteresAcumulado;

                int i = 0;




                while (i < diasInverion)

                {



                    interesMensual = montodepositoInicial * porcentajeInteres / 100;



                    i++;

                    montodepositoInicial += interesMensual + aportacionesAdicionales;

                    interesMensualAcumulado += interesMensual;


                }




                totalAnual = montodepositoInicial;

                promedioInteresAcumulado = interesMensualAcumulado / diasInverion;



                lblIntresAcumulado.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", interesMensualAcumulado);

                lblTotal.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", totalAnual);

                lblInteresAcumuladoFrecuencia.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", promedioInteresAcumulado);

                lblDepositoInicial.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", montodepositoInicialLabel);

                lblDepositos.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", aportacionesAdicionales);

            }

            catch(Exception e) {


                MessageBox.Show("Ingrese los datos correctamente" + e);

            

            }


        }


        private void txtDepositoInicial_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&

         (e.KeyChar != '.'))

            {

                e.Handled = true;

            }


            // only allow one decimal point

            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))

            {

                e.Handled = true;

            }

        }


        private void txtTasaIntresAnual_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&

        (e.KeyChar != '.'))

            {

                e.Handled = true;

            }


            // only allow one decimal point

            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))

            {

                e.Handled = true;

            }

        }


        private void txtAportacionesAdicionales_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&

        (e.KeyChar != '.'))

            {

                e.Handled = true;

            }


            // only allow one decimal point

            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))

            {

                e.Handled = true;

            }

        }


        private void txtYearsInvertir_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&

        (e.KeyChar != '.'))

            {

                e.Handled = true;

            }


        }


      


        private void txtAportacionesAdicionales_Click(object sender, EventArgs e)

        {

            txtAportacionesAdicionales.Text = string.Empty;

        }


        private void txtDepositoInicial_Click(object sender, EventArgs e)

        {

            txtDepositoInicial.Text = string.Empty;

        }


        private void txtTasaIntresAnual_Click(object sender, EventArgs e)

        {

            txtTasaIntresAnual.Text = string.Empty;

        }


        private void txtYearsInvertir_Click(object sender, EventArgs e)

        {

            txtYearsInvertir.Text = string.Empty;

        }

    }

}


Comentarios

Lo mejor de Pixels Price

CREA TU PROPIO TEAM VIEWER CON C# (PARTE 3) CONTROLA EL TECLADO

Crear códigos de barras con C#

Crear documentos PDF con C#