C#基础数学,有时想使用小数

C#基础数学,有时想使用小数,c#,math,C#,Math,我有3个文本框,有时我想使用小数作为示例: box1 3.5 box2 30 box3 12.56 这是我目前使用的代码,它只适用于整数 int box1 = int.Parse(Number1TextBox.Text); int box2 = int.Parse(Number2TextBox.Text); int box3 = int.Parse(Number3TextBox.Text); int answer = box1 * box2 * box3; AnswerLabel.Text

我有3个文本框,有时我想使用小数作为示例:

box1 3.5
box2 30
box3 12.56
这是我目前使用的代码,它只适用于整数

int box1 = int.Parse(Number1TextBox.Text);
int box2 = int.Parse(Number2TextBox.Text);
int box3 = int.Parse(Number3TextBox.Text);

int answer = box1 * box2 * box3;

AnswerLabel.Text = "The answer is"+ answer.ToString();

整数没有分数。如果希望使用带小数的可靠数值,请使用

decimal box1 = decimal.Parse(Number1TextBox.Text);
decimal box2 = decimal.Parse(Number2TextBox.Text);
decimal box3 = decimal.Parse(Number3TextBox.Text);

decimal answer = box1 * box2 * box3;
您可以按如下方式对结果进行取整:

decimal rounded = Math.Round(answer);
    double box1 = double.Parse(Number1TextBox.Text);
    double box2 = double.Parse(Number2TextBox.Text);
    double box3 = double.Parse(Number3TextBox.Text);

    double answer = box1 * box2 * box3;


    AnswerLabel.Text = "The answer is"+ answer.ToString();

首先,您要将字符串解析为整数,这些整数就是整数,首先,您需要执行以下操作:

decimal rounded = Math.Round(answer);
    double box1 = double.Parse(Number1TextBox.Text);
    double box2 = double.Parse(Number2TextBox.Text);
    double box3 = double.Parse(Number3TextBox.Text);

    double answer = box1 * box2 * box3;


    AnswerLabel.Text = "The answer is"+ answer.ToString();

接下来需要注意的是本地化,一些国家允许,作为十进制分隔符,一些国家只允许使用

如果你需要小数,为什么不直接使用十进制和
十进制。如果你需要小数,就用Parse
代替int?你有什么问题吗?“我想用小数”那么为什么不使用呢?double不太可靠,因为它是一个浮点数。这个很好用!现在我只希望答案是整数和四舍五入。我想在答案后面放一个文本示例答案是30欧元使用Math.Round-to-Round数字msdn.microsoft.com/en-us/library/system.Math.Round(v=vs.110).aspx并且要在字符串中添加欧元符号,请执行以下操作:“您的字符串”+“€”可以使用
Math.Round
进行舍入。你评论的最后一部分,好吧,试试看。谢谢!但是我应该把它放在代码里的什么地方呢?小数点四舍五入=数学四舍五入(答案)@拉斯穆斯约瑟夫森——哦,得了吧,现在你只是太懒了。你需要学会如何自己解决最基本的问题。所以这里是指导你正确方向的,但它不是一个教程网站,也不是为你编写所有代码的地方。