c# - 无法显示所有十进制数字 C#

标签 c# parsing math double

我在 C# 中有一个小方法,目的是解决这个基本方程:

Base equation

我手动给 nx

我们假设X值为3n值为1。如果我计算方程式,我会得到这个结果:

enter image description here 我的问题是输出为 0,我也尝试解析结果,但仍然是 0。

实际结果是0.88888888,但在程序输出中我只是得到了0

这是我的代码:

using System;

namespace Polinomio
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 3;
            int n = 1;

            double result = 0;

            for (int i = 0; i <= n; i++) {
                result += (double)(Math.Pow((x - 1) / 3, Math.Pow(2, i))) / Math.Pow(2, i);           
            }

            Console.WriteLine(result);
        }
    }
}

我不知道我做错了什么或错过了什么,我会感谢任何帮助。

最佳答案

只需将变量的数据类型更改为 double。

 double x = 3;
 int n = 1;

 double result = 0;

 for (int i = 0; i <= n; i++)
 {
      result += (Math.Pow((x - 1) / 3, Math.Pow(2, i))) / Math.Pow(2, i);
 }

 Console.WriteLine(result);

这样就可以了。

看这里:Implicitly converting int to double用于 C# 代码中的隐式转换优先级。

关于c# - 无法显示所有十进制数字 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58498118/

相关文章:

c# - 在 ASP.NET 中使用更新命令的日期格式问题

c# - Asp.net & 文件修改?

regex - 我有一个包含 9 个随机数的列表,如何使用 RegEx 查找重复项?

c - 没有算术运算符的C中的乘法

C# - 检查一个值在数组中出现的次数并查找索引

c# - 需要帮助在 asp.net 中为菜单项创建下拉菜单

javascript - 处理 XML DOM 中的空 #text

javascript - HtmlUnit:anchor.click 不会更改页面

c# - Project Euler 1 :Find the sum of all the multiples of 3 or 5 below 1000, 适用于 10 个数字,但不适用于 1000 个数字

python - 命名建议 "subtract absolute"类型函数