2017-03-13 19 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

爲什麼我的答案低估了?只是爲了讓你知道我測試過它的工作完美。 –

相關問題