【问题标题】:how to only display 2 spaces after the decimal in c#如何在c#中只显示小数点后2个空格
【发布时间】:2017-02-19 20:30:05
【问题描述】:

所以我必须为课堂制作一个 BMI 计算器,但我只需要一些帮助。

  1. 当我运行我的程序时,输入 2 个值然后计算,它会显示正确的答案,但小数点后有 8 位数字。

  2. 如果我输入除数字以外的任何输入,它会使我的程序崩溃,我该如何解决这个问题?

这是我的代码:

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;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void weightTxt_TextChanged(object sender, EventArgs e)
    {


    }

    private void button1_Click(object sender, EventArgs e)
    {
        double BMI = 0;
        double weight = 0;
        double height = 0;
        height = Double.Parse(heightTxt.Text);
        weight = Double.Parse(weightTxt.Text);
        // declaring and assigning 
        if (weight > 300 || weight < 10)
        {
            MessageBox.Show("Not a valid input.");

        }
        if (height > 2.2 || height < 0.2)
        {
            MessageBox.Show("Not a valid input.");
        }

        // checking that values meet parameters
        BMI = weight / (height * height);
        string result = Convert.ToString(BMI);
        resultLbl.Text = "Your BMI is : " + result;

【问题讨论】:

  • string result = BMI.ToString("#.##");

标签: c# crash decimal


【解决方案1】:

要解决非数字输入崩溃的问题,可以使用 Double.TryParse 代替 Double.Parse:

if (!Double.TryParse(heightTxt.Text, out height)) {
    MessageBox.Show("Not a valid input.");
    return;
}

要解决显示到小数点后 2 位的问题,请按照其他人的评论使用 BMI.ToString("#.##")

【讨论】:

  • 这行得通,但我不知道为什么大声笑谢谢,我会查看文档
【解决方案2】:

如果在 stackoverflow 上的每个作业都有五分钱的话,男孩。

            double BMI = 0;
            double weight = 0;
            double height = 0;

            //use try parse to test if the value is convertable
            if (!Decimal.TryParse(heightTxt.Text,out height))
            {
                MessageBox.Show("Not a valid input.");
                return;
            }

            if (!Decimal.TryParse(weightTxt.Text, out weight))
            {
                MessageBox.Show("Not a valid input.");
                return;
            }

            // declaring and assigning 
            if (weight > 300 || weight < 10)
            {
                MessageBox.Show("Not a valid input.");
                //return so the method dosnt try to do the rest of the code
                return;

            }
            if (height > 2.2 || height < 0.2)
            {
                MessageBox.Show("Not a valid input.");
                return;
            }

            BMI = weight / (height * height);
            //this is how you format the nuumber to two decimal places
            string result = BMI.ToString("#.##");
            resultLbl.Text = "Your BMI is : " + result;

【讨论】:

    【解决方案3】:
            double weight = 0;
            double height = 0;
    
            if (Double.TryParse(weightTxt.Text, out weight) && Double.TryParse(heightTxt.Text, out height)) {
                if (weight > 300 || weight < 10 || height > 2.2 || height < 0.2)
                {
                    MessageBox.Show("Not a valid input.");
                }
    
                double BMI = weight / (height * height);
    
                /*Two ways for converting to two didges:*/
    
                // 1.
    
                // Round to two didgets
                double result = Math.Round(BMI, 2);
                // convert result to string
                string resultString = Convert.ToString(result);
    
                // 2.
                string resultString = BMI.ToString("#.##")               
            }       
    

    【讨论】:

      猜你喜欢
      • 2023-01-02
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 2013-02-23
      • 2019-03-11
      • 2016-10-04
      • 2019-02-01
      相关资源
      最近更新 更多