Convert....">

Convert string [] to decimal [], int [], float [] .double [] in C #

string[] txt1 = new string[]{"12","13"};

this.SetValue(txt1, v => Convert.ChangeType(v, typeof(decimal[]), null));

it throws an error - the object must implement IConvertible.

I also need code to convert the string [] To Decimal [], int [], float [] .double []

+3
source share
1 answer

You cannot convert the string [] directly to decimal [], as it is - all elements must be individually converted to a new type. You can use Array.ConvertAll instead

string[] txt1 = new string[]{"12","13"};
decimal[] dec1 = Array.ConvertAll<string, decimal>(txt1, Convert.ToDecimal);

Similarly, using Convert.ToInt32, Convert.ToSingle, Convert.ToDoublefor the argument Converter<TInput,TOutput>for creating int [], float [], double [], by substituting the correct type arguments ConvertAll

EDIT: Silverlight, ConvertAll, :

decimal[] dec1 = new decimal[txt1.Length];
for (int i=0; i<txt1.Length; i++) {
    dec1[i] = Convert.ToDecimal(txt1[i]);
}
+12

Source: https://habr.com/ru/post/1723463/


All Articles