I have multiple double
values that need to be converted into a float array
or int array
, what should I do?
E.g.
double[] values= new double[]{123.456,234.123};
float[] floatValues=xxxx(values);
I have multiple double
values that need to be converted into a float array
or int array
, what should I do?
E.g.
double[] values= new double[]{123.456,234.123};
float[] floatValues=xxxx(values);
You'll have to do it element-wise. There are multiple ways to achieve that.
You could foreach over the source array like
floatvalues = new float[values.Length];
for(var i = 0;i < values.Length;++i)
floatValues[i] = (float)values[i];
You could use LINQ for simplifying the loop
floatValues = values.Select(d => (float)d).ToArray();
You can use Cast<T>()
floatValues = values.Cast<float>().ToArray();
You can easily do this with Linq:
using Sytem.Linq;
double[] values= new double[]{123.456,234.123};
float[] floatValues= values.Cast<float>().ToArray();
Alternet way is to use Array.ConvertAll()
,
Converts an array of one type to an array of another type.
float[] floatValues = Array.ConvertAll(values, float.Parse);