Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
Edited 10-Aug-11 19:50pm
v2
Comments
walterhevedeich at 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 at 11-Aug-11 1:54am
   
you are right.but I want to use that array to make a bitmap
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
 }
 
Smile | :)
  Permalink  
Comments
C_Johnson at 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
 
myInt32List.Add((int)item);
 
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: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 480
1 OriginalGriff 258
2 Bhavik_Patel 170
3 PhilLenoir 155
4 Richard Deeming 140
0 Sergey Alexandrovich Kryukov 8,688
1 OriginalGriff 7,237
2 CPallini 2,598
3 Richard MacCutchan 2,080
4 Abhinav S 1,888


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 11 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100