how to convert double array into integer in C#?

2020-05-02 10:26发布

问题:

I am using C# and I have a List like this List<double[]>, I want convert into integer and store all elements in another list like this List<int[]>.

How can I do this?

回答1:

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)
{
    Console.WriteLine(d);
}
Console.WriteLine("------------------------");
integers = doubles.Select(d => (int) d).ToList(); // EVERYTHING IS DONE HERE
foreach (var i in integers)
{
    Console.WriteLine(i);
}

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