如何获取格式输出 [英] How to Get the output to format

查看:51
本文介绍了如何获取格式输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获取字符串输出以格式化数学公式,例如2 ^ 4 = 16

听说我正在处理代码,也许您可​​以帮助我告诉我我做错了什么,或者如果我错过了如何格式化代码




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




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 = ("");
        }
    }

推荐答案

有时,如果给定的数字包含小数,结果可能包含几个小数,我认为在这种情况下可以使用以下内容
Sometimes, if the given number is having decimals, the result may have several decimals, I think the following can be used in such case
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

lblresult.Text = string.Format("{0:.###}^{1:.###}={2:.####}",baseNum,power,result);


怎么样:
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?


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 = ("");
        }
    }
}


这篇关于如何获取格式输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆