简体   繁体   中英

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. 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? 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM