1

As the title says, I would like to know if there is a simple way to convert a multiple dimension array of double numbers to the same array, but in int numbers.

Of course we could have two(or more) for loops going to each number and convert them, but I was wondering if there is a simple method to do it? :)

(By the way I am truly sorry if this question has already been asked a lot, but I didn't find any answer!)

Edit: As I lack a lot of informations: I have for example

double[,] tab1 = {{3.42,1.6523,42.42142},{42.124,932.241, 9.421}};
int[,] tab2;

And I would like to have at the end

tab2 = {{3,1,42}{42,932,9}}

Right now the code I have to do this is

for(int i=0; i<tab1.GetLength(0); i++){
    for (int j=0; j<tab1.GetLength(1); j++) {
        tab2[i,j] = (int)tab1[i,j];
    }
}
Sanimys
  • 101
  • 1
  • 10

1 Answers1

3

Well considering it's a two dimensional array, you can do it using a single for loop and using Array.ConvertAll() method. See an example below. hope gives a pointer

int[] convertedArray = Array.ConvertAll(myDoubleArray, x => (int)x);
Rahul
  • 76,197
  • 13
  • 71
  • 125