在C#中将字符串数组转换为浮点数组

时间:2018-08-24 16:56:10

标签: c# arrays string floating-point double

理想情况下,尝试运行此代码时出现错误,将e.Messagestring更改为doublefloat array。请帮助我们了解错误的含义或如何调试代码。

我们也厌倦了float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);方法,这也导致了错误。

private void ClientReceiveData(object sender, ConnectedClient.NetDataEventArgs e)
{
    if (string.IsNullOrEmpty(e.Message) == false)
    {
        if (e.ID == 0)
        {
            //float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);
            Array.ConvertAll(e.Message.Split(','), Double.Parse);
            Trace.WriteLine(String.Join(Environment.NewLine, e.Message));
        }

        if (e.ID == 1)
        {
            //float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);
            Array.ConvertAll(e.Message.Split(','), Double.Parse);
            Trace.WriteLine(String.Join(Environment.NewLine, e.Message));
        }
    }

输入(例如消息):

0.0160163, -0.3207418, -0.2747217, 0.9856872, -0.3428816, 0.02380935, 0.8472792, -0.3562718, 0.3080477, 0.6931204, -0.4135765, 0.2335226, 0.6617407, -0.2861188, 0.278595, 0.685502, -0.3337714, 0.06315409, 0.5661756, -0.5338272, 0.1274863, 0.6014508, -0.6020046, 0.149146, 0.6165056, -0.2287867, 0.108688, 0.753313, -0.2801398, 0.1555204, 0.5711947, -0.4046459, 0.3197014, 0.6477473, -0.4134837, 0.3147845, 0.6426268, -0.3393434, -0.242843, 0.9667573, -0.3945386, -0.2511228, 0.5462032, -0.4459318, -0.2613428, 0.2010812, -0.43355, -0.2804166, 0.09203719, -0.2818186, -0.2890762, 0.9426621, -0.2850094, -0.2625419, 0.5789056, -0.2990395, -0.2414854, 0.1805503, -0.2872439, -0.2554213, 0.06843466, -0.3543724, 0.2399589, 0.7335942, -0.5984071, 0.1476627, 0.6094998, -0.5848852, 0.1458522, 0.6007382, -0.4560334, 0.3629844, 0.6141819, -0.4130909, 0.2523528, 0.57725,

error

2 个答案:

答案 0 :(得分:5)

最后一个条目是一个空字符串,不会用Double.Parse进行解析。

您可以使用:

var parsed = Array.ConvertAll(
    e.Message.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries),
    Double.Parse);

还要注意,Double.Parse取决于当前的CultureInfo来解释字符串中的句点.

答案 1 :(得分:1)

您需要能够捕捉到令人反感的值,并且应该忽略“空”条目,例如尾随逗号创建的条目:

private void ClientReceiveData(object sender, ConnectedClient.NetDataEventArgs e)
{
    double[] result = null; 
    if (string.IsNullOrEmpty(e.Message) == false)
    {
        if (e.ID == 0)
        {
            //float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);
            result = Array.ConvertAll(e.Message.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries), ParseWithError);
            Trace.WriteLine(String.Join(Environment.NewLine, e.Message));
        }

        if (e.ID == 1)
        {
            //float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);
            result = Array.ConvertAll(e.Message.Split( new [] {','}, StringSplitOptions.RemoveEmptyEntries), ParseWithError);
            Trace.WriteLine(String.Join(Environment.NewLine, e.Message));
        }
    }
}

double ParseWithError(string value) 
{
     double result;
     if (!double.TryParse(value, out result))
     {
         throw new ApplicationException("Error parsing value " + value,e);
     }
     return result;
}