【问题标题】:Not getting the expected output (compound interest calculator)没有得到预期的输出(复利计算器)
【发布时间】:2020-05-28 18:49:24
【问题描述】:

如果我输入 £100、0.10、10(10 年),预期输出应该是 £259.37,但我得到 100.00000001000001

using System;
using System.Security.Cryptography;

namespace compound_interest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter the amount of money that is being compounded! ");
            double P = Convert.ToDouble(Console.ReadLine());
            Console.Write("Please enter the amount of interest (write it is 0.10 for 10% etc!! ");
            double MP2 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Please enter the number of years");
            double Y = Convert.ToDouble(Console.ReadLine());
            double MP1 = Convert.ToDouble(Math.Pow(MP2, Y));
            double MP = (MP1) + 1;
            double A = (P) * (MP);
            Console.WriteLine(A);
        }
    }
}

【问题讨论】:

  • 是的,使用decimal 而不是double
  • 尝试使用小数,由于 Math.Pow 不得不改为双精度
  • 嗯,这种不精确性是double 类型所固有的。 decimal 是解决它的方法,或者您可以将所有内容乘以 100 并改用整数计算。
  • 因为我需要它来平方 MP2,答案将是我乘以 A 得到答案(兴趣量)
  • 我没有得到你对 0.1^10 的期望?我怀疑你想要(1 + 利息)^years * 数量而不是你拥有的代码......但这不是一个编程问题......

标签: c#


【解决方案1】:

在你这样做之前

 double MP1 = Convert.ToDouble(Math.Pow(MP2, Y));

这样做

 MP2 = 1 + MP2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2020-09-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多