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).
Well you’re not getting any decimal values because
BigIntegeronly represents integers.Is
decimalbig 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.