Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How would i get the string output to format a math equation such as 2^4=16

hear is the code im working on maybe you can help me tell me what i am doing wrong or if i am missing how to format the code




C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // Declare variables
                int baseNum = int.Parse(basetxt.Text);
                int power = int.Parse(powertxt.Text);

                // Declare variable to hold value
                int result = CalculatePower(baseNum, power);

                // Send resul
                lblresult.Text = result.ToString("n");
            }
            catch
            {
                // Display message
                MessageBox.Show("Please enter numerical values ONLY.", "Error");
            }
         }
        private int CalculatePower(int baseNum, int power)
        {
            // for loop calculation
            int total;
            for (int i = 1; i < power; i++)
                total = power * baseNum;
            return  baseNum * +baseNum * +baseNum * +power;
        }

        //this closes app
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // this clear the textbox and lable
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lblresult.Text = ("");
            basetxt.Text = ("");
            powertxt.Text = ("");
        }
    }
Posted

Sometimes, if the given number is having decimals, the result may have several decimals, I think the following can be used in such case
C#
lblresult.Text = string.Format("{0:.000}^{1:.000}={2:.0000}",baseNum,power,result);

If zero is not required to be printed for all decimal places then
C#
lblresult.Text = string.Format("{0:.###}^{1:.###}={2:.####}",baseNum,power,result);
 
Share this answer
 
v2
Comments
Espen Harlinn 27-Mar-12 9:20am    
5'ed!
ProEnggSoft 27-Mar-12 12:09pm    
Thank you.
leetank 27-Mar-12 9:47am    
Thanks for the help that was what i need i try it almost like that but i missed few parts thank you very much for the quick response
ProEnggSoft 27-Mar-12 12:10pm    
Happy to know that it helped you.
How about something like:
lblresult.Text = baseNum.ToString() + "^" + power.ToString() + "=" + result.ToString()

Or do you need something fancier? I'd probably use a string builder or something else, but you should be able to get the idea.

Or do you mean something totally different?
 
Share this answer
 
Comments
leetank 27-Mar-12 9:48am    
Thank you too for you sugestion it also worked
C#
try
            {
                // Declare variables
                int baseNum = int.Parse(basetxt.Text);
                int power = int.Parse(powertxt.Text);

                // Declare variable to hold value
                int result = CalculatePower(baseNum, power);

                // Send resul
                lblresult.Text = string.Format("{0:.###}^{1:.###}={2:.####}", baseNum, power, result);
            }
            catch
            {
                // Display message
                MessageBox.Show("Please enter numerical values ONLY.", "Error");
            }
         }
        private int CalculatePower(int baseNum, int power)
        {
            // for loop calculation
            int total;
            for (int i = 1; i < power; i++)
                total = power * baseNum;
            return  baseNum * +baseNum * +baseNum * +power;
        }

        //this closes app
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // this clear the textbox and lable
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lblresult.Text = ("");
            basetxt.Text = ("");
            powertxt.Text = ("");
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900