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” –