zana zana - 7 months ago 49
C# Question

how to convert double array into integer in C#?

I am using C# and I have a List like this

, I want convert into integer and store all elements in another list like this

How can I do this?

mok mok

There is a heap of ways to do this, for example you can use linq like this:

List<int> integers = new List<int>();
List<double> doubles = new List<double>();
for (int i = 0; i < 10; i++)
    doubles.Add(i + new Random().NextDouble());
foreach (var d in doubles)
integers = doubles.Select(d => (int) d).ToList(); // EVERYTHING IS DONE HERE
foreach (var i in integers)

Or you can simply loop through the list or use an iterator and cast them explicitly.