Witam, zaczynam swoją naukę C#. Jestem w trakcie kodowania okienkowego kalkulatora. Podczas zamiany floata na stringa za pomocą .ToString() wyskakuje mi błąd: System.FormatException: „Input string was not in a correct format.”.
Dodam że przy nie używania przycisku . i operacji na liczba całkowitych problem nie występuje a kod do tego momentu działa dobrze. Wie ktoś jak skutecznie rozwiązać ten problem zmiany float na string?
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.Globalization;
namespace Calculator
{
public partial class Form1 : Form
{
string firstN, secondN;
char type = ' ';
public Form1()
{
InitializeComponent();
}
private void b0_Click(object sender, EventArgs e)
{
action("0");
}
private void b1_Click(object sender, EventArgs e)
{
action("1");
}
private void b2_Click(object sender, EventArgs e)
{
action("2");
}
private void b3_Click(object sender, EventArgs e)
{
action("3");
}
private void b4_Click(object sender, EventArgs e)
{
action("4");
}
private void b5_Click(object sender, EventArgs e)
{
action("5");
}
private void b6_Click(object sender, EventArgs e)
{
action("6");
}
private void b7_Click(object sender, EventArgs e)
{
action("7");
}
private void b8_Click(object sender, EventArgs e)
{
action("8");
}
private void b9_Click(object sender, EventArgs e)
{
action("9");
}
private void bC_Click(object sender, EventArgs e)
{
firstN = "";
secondN = "";
}
private void bAC_Click(object sender, EventArgs e)
{
type = ' ';
firstN = "";
secondN = "";
}
private void bDot_Click(object sender, EventArgs e)
{
action(".");
}
private void bAdd_Click(object sender, EventArgs e)
{
type = '+';
}
private void bSub_Click(object sender, EventArgs e)
{
type = '-';
}
private void bMul_Click(object sender, EventArgs e)
{
type = '*';
}
private void bDiv_Click(object sender, EventArgs e)
{
type = '/';
}
private void bP_Click(object sender, EventArgs e)
{
type = '^';
}
private void bScore_Click(object sender, EventArgs e)
{
switch(type)
{
case '+':
tbScore.Text = "";
tbScore.Text = (float.Parse(firstN) + float.Parse(secondN)).ToString();
break;
case '-':
tbScore.Text = "";
tbScore.Text = (float.Parse(firstN) - float.Parse(secondN)).ToString();
break;
case '*':
tbScore.Text = "";
tbScore.Text = (float.Parse(firstN) * float.Parse(secondN)).ToString();
break;
case '/':
tbScore.Text = "";
tbScore.Text = (float.Parse(firstN) / float.Parse(secondN)).ToString();
break;
}
}
private void action(string clickNum)
{
if (type == ' ')
{
firstN += clickNum;
tbScore.Text = firstN;
}
else
{
secondN += clickNum;
tbScore.Text = "";
tbScore.Text = secondN;
}
}
}
}
Z góry dziękuje :)