将字符串转换为BigInteger [英] Converting a string into BigInteger

查看:115
本文介绍了将字符串转换为BigInteger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码创建了一个很大的数字( BigInteger ),然后将其转换为 string .

I have the following code that creates a very big number (BigInteger) which is converted then into a string.

// It's a console application.
BigInteger bi = 2;
for (int i = 0; i < 1234; i++)
{
   bi *= 2;
}
string myBigIntegerNumber = bi.ToString();
Console.WriteLine(myBigIntegerNumber);

我知道,要转换为 int ,我们可以使用 Convert.ToInt32 ,而转换为 long ,我们可以使用 Convert.ToInt64 ,但是如何转换为 BigInteger ?

I know that for converting to int we can use Convert.ToInt32 and converting to long we use Convert.ToInt64, but what's about converting to BigInteger?

如何将 string (表示非常长的数字)转换为 BigInteger ?

How can I convert a string (that represents a very very long number) to BigInteger?

推荐答案

使用 BigInteger.Parse() 方法.

将指定样式的数字的字符串表示形式转换为相当于BigInteger.

Converts the string representation of a number in a specified style to its BigInteger equivalent.

BigInteger bi = 2;
for(int i = 0; i < 1234; i++)
{
    bi *= 2;
}

var myBigIntegerNumber = bi.ToString();
Console.WriteLine(BigInteger.Parse(myBigIntegerNumber));

您还可以检查 BigInteger.TryParse( )方法与您的对话是否成功.

Also you can check BigInteger.TryParse() method with your conversation is successful or not.

尝试将数字的字符串表示形式转换为其等效于BigInteger,并返回一个值,该值指示转换成功.

Tries to convert the string representation of a number to its BigInteger equivalent, and returns a value that indicates whether the conversion succeeded.

这篇关于将字符串转换为BigInteger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆