繁体   English   中英

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

[英]convert string array to float array in C#

尝试运行此代码时,我们遇到错误,理想情况下将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));
        }
    }

输入 (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,

错误

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

您可以使用:

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

另请注意, Double.Parse依赖于当前的CultureInfo来解释句点. 在字符串中。

您需要能够捕获有问题的值,并且应该忽略“空”条目,例如由尾部逗号创建的条目:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM