Home > Back-end >  C# can't convert float to string
C# can't convert float to string

Time:11-23

I'm making a simple calc and have trouble. The label text cant be float. How to fix it? (the label is a equal)

public partial class Form1 : Form
{
    string firstnum;
    string secondnum;
    string equalnum;
    float firstnumf;
    float secondnumf;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        equalnum = firstnumf   secondnumf;
        equal.Text = equalnum;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string firstnum = richTextBox1.Text;
        string secondnum = richTextBox2.Text;
        float firstnumf = float.Parse(firstnum);
        float secondnumf = float.Parse(secondnum);
    }

CodePudding user response:

I made it once completely new, "tb_" are the 2 textboxes, if there are still questions. Then ask

private void bttn_calc_Click(object sender, EventArgs e)
{
    //Create 2 double´s (is similar to float)
    double firstnum, secondnum;

    //Turns a text into a double number
    firstnum = Convert.ToDouble(tb_firstnum.Text);
    secondnum = Convert.ToDouble(tb_secondnum.Text);

    //Adds the 2 doubles and makes them into a string
    equal.Text = (firstnum   secondnum).ToString();
}

CodePudding user response:

Make your "equalnum" into a float

float equalnum;

And let it convert to a string at the end

equal.Text = equalnum.ToString();
  • Related