C# 如何在c中仅显示小数点后的2个空格#
所以我必须为课堂制作一个体重指数计算器,但我只需要一些东西的帮助C# 如何在c中仅显示小数点后的2个空格#,c#,crash,decimal,C#,Crash,Decimal,所以我必须为课堂制作一个体重指数计算器,但我只需要一些东西的帮助 当我运行程序时,输入2个值,然后计算,它显示正确的答案,但小数点后有8位数字 如果我输入了除数字以外的任何输入,程序就会崩溃,我该如何解决这个问题 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq
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;
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Windows窗体应用程序3
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void weightTxt_TextChanged(对象发送方,事件参数e)
{
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
双倍体重指数=0;
双倍重量=0;
双倍高度=0;
height=Double.Parse(heightTxt.Text);
weight=Double.Parse(weightTxt.Text);
//声明和分配
如果(重量>300 | |重量<10)
{
Show(“不是有效的输入”);
}
如果(高度>2.2 | |高度<0.2)
{
Show(“不是有效的输入”);
}
//检查值是否符合参数
体重指数=体重/(身高*身高);
字符串结果=Convert.ToString(BMI);
resultbl.Text=“您的体重指数为:”+结果;
双倍重量=0;
双倍高度=0;
if(Double.TryParse(weightTxt.Text,out-weight)和&Double.TryParse(heightTxt.Text,out-height)){
如果(重量>300 | |重量<10 | |高度>2.2 | |高度<0.2)
{
Show(“不是有效的输入”);
}
双倍BMI=体重/(身高*身高);
/*转换为两个指令的两种方式:*/
// 1.
//两个傻瓜
双结果=数学四舍五入(BMI,2);
//将结果转换为字符串
字符串resultString=Convert.ToString(结果);
// 2.
字符串结果字符串=BMI.ToString(“#.###”)
}
要解决非数字输入崩溃的问题,可以使用Double.TryParse而不是Double.Parse:
if (!Double.TryParse(heightTxt.Text, out height)) {
MessageBox.Show("Not a valid input.");
return;
}
要解决显示到小数点后2位的问题,请使用BMI.ToString(“#.#.#.#.#”)如其他人所评论的那样,如果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;
double BMI=0;
双倍重量=0;
双倍高度=0;
//使用try parse测试值是否可转换
如果(!Decimal.TryParse(heightTxt.Text,out height))
{
Show(“不是有效的输入”);
返回;
}
如果(!Decimal.TryParse(weightTxt.Text,out-weight))
{
Show(“不是有效的输入”);
返回;
}
//声明和分配
如果(重量>300 | |重量<10)
{
Show(“不是有效的输入”);
//返回,以便该方法不尝试执行代码的其余部分
返回;
}
如果(高度>2.2 | |高度<0.2)
{
Show(“不是有效的输入”);
返回;
}
体重指数=体重/(身高*身高);
//这是将数字格式化为小数点后两位的方法
字符串结果=BMI.ToString(“#.###”);
resultbl.Text=“您的体重指数为:”+结果;
string result=BMI.ToString(“#.###”)
这是有效的,但我不知道为什么。谢谢,我会查看文档
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;