BigInteger.ToString()返回超过50个十进制数字 [英] BigInteger.ToString() returns more than 50 decimal digits

查看:731
本文介绍了BigInteger.ToString()返回超过50个十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是.NET 4 系统.Numerics.BigInteger结构和我得到的结果从文档的不同。

I'm using .NET 4 System.Numerics.BigInteger Structure and I'm getting results different from the documentation.

BigInteger.ToString的文档( )方法该说:

的ToString()方法支持50   小数precision数字。那是,   如果BigInteger值已超过   50个数字中,只有50个最   显著的数字是preserved在   输出字符串;所有其他数字   替换为零。

The ToString() method supports 50 decimal digits of precision. That is, if the BigInteger value has more than 50 digits, only the 50 most significant digits are preserved in the output string; all other digits are replaced with zeros.

我有一些code,需要一个60位十进制数的BigInteger ,并将其转换为字符串。 60显著十进制数字字符串并没有失去任何显著的数字:

I have some code that takes a 60 decimal digits BigInteger and converts it to a string. The 60 significant decimal digits string didn't lose any significant digits:

const string vString = "123456789012345678901234567890123456789012345678901234567890";
Assert.AreEqual(60, vString.Length);
BigInteger v = BigInteger.Parse(vString);
Assert.AreEqual(60, v.ToString().Length);
Assert.AreEqual('9', v.ToString()[58]);
Assert.AreEqual('1', v.ToString()[0]);
Assert.AreEqual(vString, v.ToString());
Assert.AreEqual(vString, v.ToString("G"));

所有的断言通。

All the asserts pass.

这到底是什么文件的引用的部分是什么意思?

What exactly does the quoted part of the documentation mean?

推荐答案

该文档是有点不清楚这里,这个限制只适用于当格式的字符串,例如:

The documentation is a little unclear here, this limit only applies when formatting the string, for example:

v.ToString("0"); "123456789012345678901234567890123456789012345678900000000000"
v.ToString("n0"); "123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678,900,000,000,000"

该例外被格式化为R ,这使原有的往返式操作值:

The exception is formatting it as "R", which gives the original round-tripped value:

v.ToString("R"); "123456789012345678901234567890123456789012345678901234567891"

这篇关于BigInteger.ToString()返回超过50个十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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