Hello world我有一些問題試圖將包含浮點數的字符串(例如string s =「23.532」)轉換爲浮點數。請看一下。值來自.txt文件。將字符串轉換爲浮點數問題
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
強烈建議在這些場景中使用float.TryParse(string,out float)。 – celerno
可能SingleNumb沒有有效的浮點數。拋出異常時它的值是什麼? –
SingleNumb值爲「0.534」 –