将双精度数组转换为整数1

问题描述:

你好,我想将double类型的数组转换为int;应该在元素上使用循环,还是有更好的方法?如何处理一个类型的两个数组?要先感谢

Hello,I want to conver the array with type double to int;shoud I use a loop over the elements or there is a better way?what about coping two arrays of one type?thanks in advance



有许多可能的解决方案.这是一个:

Hi,

there are many possible solutions for this. Here is one:

public static int[] ConvertDoubleArrayToIntArray(double[] adDoubleArray)
{
     return adDoubleArray.Select(d => (int)d).ToArray();
}



并像这样使用:



and use like this:

double[] adDoubleArray = { 1.1d, 2.2d, 3.3d, 4.4d, 5.5d };
int[] aiIntArray = ConvertDoubleArrayToIntArray(adDoubleArray);


这可能会有所帮助,

Convert.ToInt32 [
It might be helpful,

Convert.ToInt32 [^]

an example,

static void Main(string[] args)
 {
     double[] myDoubleArray = { 114.4, 225.0, 11.7 };
     IList<Int32> myInt32List = new List<Int32>();
     Array.ForEach(myDoubleArray, item =>
     {
           myInt32List.Add(Convert.ToInt32(item));

      });
      Int32[] myInt32Array = myInt32List.ToArray();
 }



:)



:)


我认为您别无选择,只能编写一些LINQ或foreach来遍历列表并一次转换一次.
I think you have no choice but to write some LINQ or a foreach to iterate over your list and convert one at a time.