将字符串转换为浮动问题

时间:2017-03-13 21:49:55

标签: c#

Hello world我试图将包含浮点数的字符串(例如字符串s =“23.532”)转换为浮点数时遇到了一些问题。请看一下。值来自.txt文件。

Screenshot #1

Screenshot #2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Chart Properties
            chart1.ChartAreas[0].AxisX.ScaleView.Zoom(-5, 5);
            chart1.ChartAreas[0].AxisY.ScaleView.Zoom(0, 1000);
            chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;







            chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;

        }

        private void BLoad_Click(object sender, EventArgs e)
        {
            //Load File
            string SingleNumb= "";
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                StreamReader sr = new StreamReader(File.OpenRead(ofd.FileName));

                while ((SingleNumb = sr.ReadLine()) != null)
                {
                    float value = float.Parse(SingleNumb);
                    //MessageBox.Show(value.ToString()); just to correct values
                }


                   //chart1.Series[0].Points.AddXY(Single.Parse(SingleNumb), i++);


                sr.Dispose();
                sr.Close();
            }

        }
    }
}

文本文件

0.534
-0.283
4.632
-8.5325

7 个答案:

答案 0 :(得分:2)

您应该使用float.TryParse(string, float),因为它会测试您要解析的string在转换之前是否可以转换为float

试试这个:

string floatString = "23.532";
float number = 0;
if (float.TryParse(floatString, out number)) Console.WriteLine($"Number = {number}");

float.TryParse(string, float)方法将数字的string表示形式转换为其等效的单精度浮点数。返回值表示转换是成功还是失败。 true表示已成功,false表示已成功。

答案 1 :(得分:0)

如果你的字符串使用点(。)作为小数分隔符,你应该使用这个重载:

 float xFreq = Convert.ToSingle(param, CultureInfo.InvariantCulture);

不太确定,但附加信息对我来说是斯拉夫语,默认的小数点分隔符可能是逗号(,)而不是点。当然,这取决于当前线程的文化,这取决于区域设置。

答案 2 :(得分:0)

使用TryParse并检查结果。

float number;

if(float.TryParse(floatString, out number))
{
   ...
}

答案 3 :(得分:0)

在函数BLoad_Click(...)中更新while循环,如下所示:

while((SingleNumb = sr.ReadLine()) != null)
{
    NumberStyles style = NumberStyles.Any;
    CultureInfo culture = CultureInfo.InvariantCulture;

    float value = 0.0f;
    if (float.TryParse(SingleNumb, style, culture, out value))
    {
        MessageBox.Show(value.ToString());
    }
    else
    {
        MessageBox.Show("Conversion failed!");
    }
}

注意:' float.TryParse(...)' - 将数字的字符串表示形式转换为其等效的浮点数。返回值表示转换是成功还是失败。

答案 4 :(得分:0)

你可以尝试:

string SingleNumb = "23.532";  
float value = float.Parse(SingleNumb);

转化后的结果:

enter image description here

我想知道你从文件中加载的值是否是一个有效的字符串!

答案 5 :(得分:-2)

您正在寻找Convert.ToSingle()方法:

:nnoremap p p`[v`]=

答案 6 :(得分:-2)

你可以尝试一下......

string SingleNumb = "";
string[] lines = System.IO.File.ReadAllLines(@"C:\testFile.txt");

// Display the file contents by using a foreach loop.            
foreach (string line in lines)
{
     if((SingleNumb = line) != null)
     {
         float value = float.Parse(SingleNumb);
     }
} 

(*)创建一个文件" testFile.txt"在C:\中包含您拥有的值。

这应该可以按预期工作。