2017-03-13 20 views
-6

Hello world我有一些问题试图将包含浮点数的字符串(例如string 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 
+0

强烈建议在这些场景中使用float.TryParse(string,out float)。 – celerno

+1

可能SingleNumb没有有效的浮点数。抛出异常时它的值是什么? –

+0

SingleNumb值为“0.534” –

回答

0

如果你的S特林使用点作为小数点分隔符,你应该使用这个过载(。):

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

不能确定,但​​其他信息看起来斯拉夫,我和默认的小数点分隔符可以是逗号(,),而不是点。当然,这取决于当前线程的文化,这取决于区域设置。

+0

数字与点号分隔。上面的代码返回 错误\t CS0103 \t“的CultureInfo”这个名字在目前情况下 –

+0

@ DE4POW这很可能是你正在寻找......真的不知道为什么这是回答不存在downvoted ...(可能有人认为有吨“解析数作为float在波兰失败”的问题,downvoted为乐趣) –

+0

感谢您的答复;) –

-2

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

string floatString = "23.532"; 
float actualFloat = Convert.ToSingle(floatString); 
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

+0

您应该添加一些解释,说明为什么应该使用这个答案(例如为什么TryParse在这种情况下比Parse更有用)。 – Adrian

+0

@Adrian当然,我会更新我的答案 –

0

使用TryParse并检查结果。

float number; 

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

更新而功能BLoad_Click(...),如下内循环:

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(...)' - 一些字符串表示形式转换它的浮点数相当于。返回值指示转换是成功还是失败。

+0

一直失败 –

+0

SingleNumb的价值是什么?尝试调试SingleNumb的值。 –

+0

对不起,但我不是真的如此深入的C#知道如何调试呢。如果你想看,在主帖中包含整个代码。 –

0

你能尝试:从转换

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

结果:

enter image description here

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

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

(*)C语言创建一个文件 “TESTFILE.TXT”:\与你的价值观。

这应该按照您的预期工作。

+0

为什么我的答案低估了?只是为了让你知道我测试过它的工作完美。 –

相关问题