Round A List Of Decimal Lists
c#, linq, rounding
Solution
You should use `Decimal.Round`, not `Math.Round`, to do the job:
masterList = masterList.Select(
x => x.Select(y => Decimal.Round(y, 2)).ToList()
).ToList();
Edited with the number of decimals (2). Also, the problem seems to be in the `LINQ` statement instead of the use of `Math.Round` or `Decimal.Round`.
`Decimal.Round` is my preference for `decimal` case though, especially when the `decimal` value needs to be converted to the shortest `int`. The format is very handy: `Decimal.Round(val)`
Problem
I'm trying to find a shorter way of rounding each element in a `List<List<decimal>>`, and wondering if there's a way I can shorten it with Linq? I've tried several ways, such as this ``` List<List<Decimal>> masterList = dataSet .Select(x => x.Values) .ToList() .Select(i => Math.Round(i, 2)); /// THIS GIVES AN EXCEPTION (CANNOT CONVERT FROM System.Collections.Generic.List<decimal> to 'double' ``` This is my current way of doing it? ``` List<List<Decimal>> masterList = dataSet.Select(x => x.Values).ToList(); foreach (var list in masterList) { for(var i = 0; i < list.Count; i++) { list[i] = Math.Round(list[i], 2); } } ```