BigInteger 的除法和舍入
我正在尝试创建一个集合,其中的小数精度四舍五入到小数点后两位。然而,该集合有一些巨大的数字,我必须使用 BigInteger 作为解决方案。
具体内容: - 得到了一个包含 BigIntegers 的集合 - 得到了另一个 BigIntegers 集合 - 得到了第三个大整数集合 - 我必须创建一个集合,其中包含上述 3 个集合的平均值,并将值四舍五入到小数点后两位。 即,如果collection1有{2,3,4}并且collection2有{4,5,5}并且collection3有{5,3,2}我应该创建一个具有{3.67,3.67,,3.67}的第四个集合
为此我正在使用此代码:
BigInteger divisor = new BigInteger(3.0d);
var averages = collection1.Zip(collection2, BigInteger.Add)
.Zip(collection3,
(xy, z) => BigInteger.Divide(BigInteger.Add(xy,z), divisor));
但是小数点没有出现。我不确定 biginteger 是否只能保存整数值而不能保存小数值。
您能为此建议一个解决方案吗?
注意:它必须是基于 LINQ 的,因为集合非常大,并且有一些大值(因此是大整数)。
I'm trying to create a collection which has decimals with precision rounded to 2 decimal places. However the collection have some huge numbers and I have to use the BigInteger as the solution.
The specifics:
- Got a collection which has BigIntegers
- Got another collection of BigIntegers
- Got a third collection of Big integers
- I have to create a collection which has average value of the above 3 collections, with values rounded to 2 decimal places.
i.e if collection1 has {2,3,4} and collection2 has {4,5,5} and collection3 has {5,3,2} I should create a 4th collection which has {3.67,3.67,,3.67}
For this I'm using this code:
BigInteger divisor = new BigInteger(3.0d);
var averages = collection1.Zip(collection2, BigInteger.Add)
.Zip(collection3,
(xy, z) => BigInteger.Divide(BigInteger.Add(xy,z), divisor));
However the decimals are not appearing. I'm not sure as to whether biginteger can hold only integer values and not decimal values.
Can you please suggest a solution for this?
Note: It has to be LINQ based as the collections are pretty huge with some big values(and hence biginteger).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您没有得到任何十进制值,因为 BigInteger 仅代表整数。
decimal
是否足够大以容纳您感兴趣的数字?如果没有,您可能需要考虑将所有内容乘以 100,并修复格式方面,使“1500”显示为“15.00”等。您仍然需要做一些工作才能最终得到“.67”而不是三分之二结果的“.66”,因为这将是截断而不是四舍五入时除法的自然结果。
Well you're not getting any decimal values because
BigInteger
only represents integers.Is
decimal
big enough to hold the number you're interested in?If not, you might want to consider multiplying everthing by 100, and fixing the formatting side such that "1500" is displayed as "15.00" etc. You'd still need to do a bit of work to end up with ".67" instead of ".66" for a two-thirds result, as that would be the natural result of the division when it's truncated instead of rounded.
除以三后的余数将为 0.00、0.33 或 0.67。您应该能够确定这三个值中哪一个是合适的。但是,我不确定您希望如何让您的集合存储内容,因为支持分数的数字类型将无法保存您的结果,除非您想定义“大整数加小分数”类型或将您的数字存储为字符串。
The residue after division by three is going to either be .00, .33, or .67. You should be able to determine which of those three values is appropriate. I'm not sure how you will want to have your collection store things, however, given that the numerical types that support fractions won't be able to hold your result, unless you want to define a "big integer plus small fraction" type or store your numbers as strings.