Reputation: 3284
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).
Upvotes: 2
Views: 2215
Reputation: 81217
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.
Upvotes: 0
Reputation: 1502186
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.
Upvotes: 3