Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i need to convert Decimal array to string array . How to convert decimal[] to string[] ? Can i use

Array.ConvertAll()

method to do this task?

share|improve this question
You'll have to create new array finnaly, so just iterate over this array. – Guy Apr 18 at 6:02
@Guy: Well Array.ConvertAll does that for you... – Jon Skeet Apr 18 at 6:04
It's not a big effort to try it or read the documentation: msdn.microsoft.com/pl-pl/library/exc45z53.aspx. Anyhow, the method will work. – Dawid Dziadkiewicz Apr 18 at 6:05
@JonSkeet Yes I know, your solution is great! – Guy Apr 18 at 6:05

3 Answers

up vote 3 down vote accepted

Of course you can use Array.ConvertAll method. You just need a conversation which can be done easyly with lambda expression.

string[] string_array = Array.ConvertAll(decimal_array, x => x.ToString());

Array.ConvertAll converts an entire array. It converts all elements in one array to another type.

Let's code it;

decimal[] decimal_array = new decimal[] {1.1M, 1.2M, 1.3M, 1.4M };
string[] string_array = Array.ConvertAll(decimal_array, x => x.ToString());

foreach (var item in string_array)
{
      Console.WriteLine("{0} - {1}", item.GetType(), item);
}

Output will be;

System.String - 1.1
System.String - 1.2
System.String - 1.3
System.String - 1.4

Here is a DEMO.

share|improve this answer

Try this

decimal[] decArr = new decimal[5];
// ...
string[] strArr = decArr.Select(d => d.ToString("0.00")).ToArray();

Hope this helps

share|improve this answer

Yes, you can use Array.ConvertAll pretty simply - you just need to provide the conversion delegate, which is most easily done with a lambda expression:

string[] strings = Array.ConvertAll(numbers => x => x.ToString());

Slightly less efficiently, but more idiomatically and flexibly, you can use LINQ:

string[] strings = numbers.Select(x => x.ToString()).ToArray();

Or if you don't actually need an array, and are happy for it to perform the string conversion every time you iterate over it:

IEnumerable<string> strings = numbers.Select(x => x.ToString());

The flexibility here is that numbers can change to be any IEnumerable<decimal> - so if you change to using a List<decimal>, you won't need to change this conversion code, for example.

The slight loss in efficiency when calling ToArray is that the result of calling Select is a lazily-evaluated sequence which doesn't know its size to start with - so it can't know the exact size of output array immediately, whereas ConvertAll obviously does.

share|improve this answer
+1 for why ConvertAll is more efficient. I was about to ask that but just saw your edit – Habib Apr 18 at 6:08
How is this possible this questions isn't accepted? :) This is much more detailed. – Soner Gönül Apr 18 at 6:31
2  
@SonerGönül it is probably an attempt to stop Jon's I-get-it-all experience :) – TheBlastOne Apr 18 at 6:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.