【问题标题】:How to deal with big numbers如何处理大数字
【发布时间】:2018-09-13 02:22:43
【问题描述】:

我目前正在实施一个空闲的点击游戏,但我遇到了大数字的问题。 我正在使用 Unity 和 C#。

如何处理大数字?

我已经使用了一个处理大数字的函数,但它有一个限制(整数限制)。我想要一个没有限制的功能。

if (rawNumber < 1000000.0) {
        return rawNumber.ToString (formatString);
    }
    ScientificNotation scientificNotation = ScientificNotation.FromDouble (rawNumber);
    ushort adjustedExponent = (ushort)((scientificNotation.exponent / 3) - 1);
    string prefix = "";
    if (adjustedExponent < 10) {
        prefix = latin[adjustedExponent - 1];
    }
    else {
        ushort hundredsPlace = (ushort)(adjustedExponent / 100);
        ushort tensPlace = (ushort)((adjustedExponent / 10) % 10);
        ushort onesPlace = (ushort)(adjustedExponent % 10);
        string onesString = (onesPlace > 0) ? ones[onesPlace - 1] : "";
        string modifier = "";
        if ((onesPlace == 7) || (onesPlace == 9)) {
            if (tensPlace > 0) {
                if ((tensPlace == 2) || (tensPlace == 8)) {
                    modifier = "m";
                }
                else if (tensPlace != 9) {
                    modifier = "n";
                }
            }
            else if (hundredsPlace > 0) {
                if (hundredsPlace == 8) {
                    modifier = "m";
                }
                else if (hundredsPlace != 9) {
                    modifier = "n";
                }
            }
        }
        if ((onesPlace == 3) || (onesPlace == 6)) {
            if (tensPlace > 0) {
                if ((tensPlace == 2) || (tensPlace == 3) || (tensPlace == 4) || (tensPlace == 5) || (tensPlace == 8)) {
                    modifier = ((onesPlace == 6) && (tensPlace == 8)) ? "x" : "s";
                }
            }
            else if (hundredsPlace > 0) {
                if ((hundredsPlace == 1) || (hundredsPlace == 3) || (hundredsPlace == 4) || (hundredsPlace == 5) || (hundredsPlace == 8)) {
                    modifier = ((onesPlace == 6) && ((tensPlace == 1) || (tensPlace == 8))) ? "x" : "s";
                }
            }
        }
        string tensString = (tensPlace > 0) ? tens[tensPlace - 1] : "";
        string hundredsString = (hundredsPlace > 0) ? hundreds[hundredsPlace - 1] : "";
        prefix = string.Format ("{0}{1}{2}{3}", onesString, modifier, tensString, hundredsString);
    }

    double adjustedSignificand = scientificNotation.significand * Math.Pow (10, scientificNotation.exponent % 3);
    double integralPart = Math.Truncate (adjustedSignificand);
    //return string.Format("{0} {1}llion", (((adjustedSignificand - integralPart) > maxFractional) ? integralPart + maxFractional : adjustedSignificand).ToString (formatString), prefix.TrimEnd ('a'));
    return string.Format("{0} {1}", (((adjustedSignificand - integralPart) > maxFractional) ? integralPart + maxFractional : adjustedSignificand).ToString(formatString), prefix);
    //return "" + " " + "";

}

我该怎么办?

我的“解决方案”是拥有一个 int 数组并实现一些使用不同数组位置的逻辑(还不知道逻辑)。例如,我将从 [0] 开始。当该 int 达到最大值时,我将开始使用 [1]。

我应该如何处理它出现 1000 = 1k 的方式。 1000000 = 1M.. 1 aa、1 ab、1 ac 等

有人可以帮我或给我小费吗?

非常感谢。

【问题讨论】:

  • 试试 System.Numerics.BigInteger.
  • 数字有多大?精确到什么程度?长让你 +/- 9 乘以 10 到 18 (数着我的手指)。一个小数可以获得 128 位的数字,范围很广。双打牺牲精度来换取射程。为什么要一个“大数字”?
  • 具有大数字的空闲点击游戏通常使用 double 作为其值的基本类型。即使它们使用自然数运行,但它们的范围直到 ±1.7 × 10^308 通常对它们来说已经足够了。
  • 我没有统一的 BigInteger,我尝试了 System.Numerics.BigInteger,但它不起作用。
  • 我应该如何处理它出现 1000 = 1k 的方式。 1000000 = 1M.. 1 aa、1 ab、1 ac 等

标签: c# integer


【解决方案1】:

看看这个:C# Integral types table

就像 Leandro 已经提到的:

使用 System.Numerics.BigInteger 类来表示任意大的有符号整数。

【讨论】:

  • 当我这样做时,我得到一个错误。 “命名空间‘System’中不存在类型‘Numerics’”
  • 您必须在项目中添加对 System.Numerics 的引用。单击添加引用、程序集、框架并选择 System.Numerics。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多