[英]Round a number using Math.Round with MidpointRounding.AwayFromZero
I need to get a number from a text box and divide that number by 5.5 in another text box. 我需要从一个文本框中获取一个数字,然后在另一个文本框中将该数字除以5.5。 The answer needs to be rounded up to the nearest whole. 答案需要四舍五入到最接近的整数。 The problem I'm having is how do I implement the Math.Round using text boxes? 我遇到的问题是如何使用文本框实现Math.Round? Below is how I tried to get it to work. 以下是我尝试使其工作的方式。
double num2 = Math.Round(Convert.ToDouble(textBox5.Text,1,0)) / 5.5;
textBox6.Text = num2.ToString();
double num2 = Math.Round((Convert.ToDouble)textBox5.Text / 5.5);
double num2 = Math.Ceiling(Convert.ToDouble(textBox5.Text,0.00(MidpointRounding.AwayFromZero))) / 5.5;
textBox6.Text = num2.ToString();
I would break it down to a single operation per line of code and do it this way: 我将其分解为每行代码的单个操作,并以此方式进行操作:
var x = Double.Parse(textBox5.Text);
x = x / 5.5;
x = Math.Round(x, MidpointRounding.AwayFromZero);
textBox6.Text = x.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.