12,511,920 members (52,084 online)
Rate this:
See more:
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
Posted 10-Aug-11 19:49pm
Updated 10-Aug-11 19:50pm
v2
walterhevedeich 11-Aug-11 1:52am

I guess you have to loop over the elements. Just curious, why would you want to do that? By converting it to int, you have decreased the precision of the data type.
asma_from_yazd 11-Aug-11 1:54am

you are right.but I want to use that array to make a bitmap

Rate this:

## Solution 3

Hi,

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

```public static int[] ConvertDoubleArrayToIntArray(double[] adDoubleArray)
{
}```

and use like this:

```double[] adDoubleArray = { 1.1d, 2.2d, 3.3d, 4.4d, 5.5d };
v2
Rate this:

## Solution 2

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 =>
{

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

:)
C_Johnson 20-Sep-11 16:57pm

Converting an integer to a double using Convert.ToInt32 is not a time efficient method. Although it does work it is not recommended, using a cast will do suffice e.g

This will allow the value to be directly copied to a new memory position. The reason you need to use the cast is because converting from double to a integer will cause a loss of data the cast says you accept this loss. The Convert.ToInt32 requires the data to be a transferred and operated on externally to produce the same results. In this case it is superfluous to the outcome.
Rate this:

## Solution 1

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.

Top Experts
Last 24hrsThis month
 David_Wimbley 170 Maciej Los 100 John Simmons / outlaw programmer 85 OriginalGriff 85 Dave Kreskowiak 75
 Maciej Los 80 David_Wimbley 25 OriginalGriff 25 Dave Kreskowiak 25 Rambali Singh 20