BigInteger 字符串结果的科学表示法

问题描述 投票:0回答:1

在 C# 中,很长一段时间以来,我一直在尝试执行一个函数,该函数可以从输入中获取科学记数法中的数字,例如:1.23e25,然后从该数字将其转换为字符串,因此输出将是: 12300000000000000000000000。当我们得到字符串后,我们可以将其直接转换为BigInteger 我有这段代码,所以我希望这个例子给出真正的大整数:

MessageBox.Show(ParseScientificNotationToBigInteger("1.23e10").ToString());    // Ex 1
MessageBox.Show(ParseScientificNotationToBigInteger("1e10").ToString());       // Ex 2
MessageBox.Show(ParseScientificNotationToBigInteger("-1e10").ToString());      // Ex 3
MessageBox.Show(ParseScientificNotationToBigInteger("1e-10").ToString());      // Ex 4
MessageBox.Show(ParseScientificNotationToBigInteger("1e+10").ToString());      // Ex 5
MessageBox.Show(ParseScientificNotationToBigInteger("1.5e-10").ToString());    // Ex 6

我想要将字符串中的科学记数法转换为 C# 中的 BigInteger 数字的工作函数,因为我目前正在研究黑洞模拟,使用科学记数法可以更容易,例如:G 常数:6.67430e-11 和黑色质量孔等

编辑:我有 ParseScientificNotationToBigInteger 但它不起作用:

private BigInteger ParseScientificNotationToBigInteger(string input)
    {
        // Check if the input string matches the scientific notation format
        if (Regex.IsMatch(input, @"^[+-]?(\d+(\.\d*)?|\.\d+)?([eE][+-]?\d+)?$"))
        {
            // Remove the 'e' or 'E' from the scientific notation
            string[] parts = input.Split('e', 'E');
            string mantissa = parts[0];
            int exponent = parts.Length > 1 ? int.Parse(parts[1]) : 0;

            // Remove dots from the mantissa
            mantissa = mantissa.Replace(".", "");

            // Create a BigInteger from the mantissa
            BigInteger result = BigInteger.Parse(mantissa);

            // Calculate the new exponent
            int newExponent = exponent - mantissa.Length + mantissa.TrimStart('0').Length;

            // Raise the number to the power according to the new exponent
            result *= BigInteger.Pow(10, newExponent);

            return result;
        }

        throw new ArgumentException("Invalid scientific notation format.");
    }
c# biginteger scientific-notation
1个回答
0
投票

我认为您需要将函数名称从

ParseScientificNotationToBigInteger
更改为
ParseScientificNotationToString
,因为您想要/需要为整数以外的其他值生成结果。

一种快速方法(=它可以写得更好......😉):

String ParseScientificNotationToString(string input)
{
    // Check if the input string matches the scientific notation format
    if (Regex.IsMatch(input, @"^[+-]?(\d+(\.\d*)?|\.\d+)?([eE][+-]?\d+)?$"))
    {
        // Remove the 'e' or 'E' from the scientific notation
        string[] parts = input.Split('e', 'E');
        string mantissa = parts[0];
        int exponent = parts.Length > 1 ? int.Parse(parts[1]) : 0;

        // Remove dots from the mantissa
        mantissa = mantissa.Replace(".", "");

        // Create a BigInteger from the mantissa
        BigInteger result = BigInteger.Parse(mantissa);

        // Calculate the new exponent
        int newExponent = exponent - mantissa.Length + mantissa.TrimStart('0').Length;

        // Raise the number to the power according to the new exponent
        if (newExponent < 0)
        {
            return (exponent<0?"-":"")+ "0." + new string('0',-newExponent+1) + Math.Abs(exponent);
        }
        else
        {
            result *= BigInteger.Pow(10, newExponent);
            return result.ToString();
        }
    }
    throw new ArgumentException("Invalid scientific notation format.");
}

您的例子:

// Because my default decimal separator is a ',', I needed this:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

Console.WriteLine(ParseScientificNotationToString("1.23e10"));    // Ex 1
Console.WriteLine(ParseScientificNotationToString("1e10").ToString());       // Ex 2
Console.WriteLine(ParseScientificNotationToString("-1e10").ToString());      // Ex 3
Console.WriteLine(ParseScientificNotationToString("1e-10").ToString());      // Ex 4
Console.WriteLine(ParseScientificNotationToString("1e+10").ToString());      // Ex 5
Console.WriteLine(ParseScientificNotationToString("1.5e-10").ToString());    // Ex 6

结果:

1230000000000
10000000000
-10000000000
-0.0000000000010
10000000000
-0.0000000000010
© www.soinside.com 2019 - 2024. All rights reserved.