简体   繁体   English

使用Math.Round和MidpointRounding.AwayFromZero四舍五入一个数字

[英]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.

相关问题 JavaScript中的Math.round MidPointRounding.AwayFromZero - Math.round MidPointRounding.AwayFromZero in javascript 与Delphi中的MidpointRounding.AwayFromZero相当的Math.Round()是什么? - What is the equivalent of Math.Round() with MidpointRounding.AwayFromZero in Delphi? Math.Round bug 81.725 MidpointRounding.AwayFromZero = 81.72 - Math.Round bug 81.725 MidpointRounding.AwayFromZero = 81.72 使用Math.Round(ReportItems!category.Value,MidpointRounding.AwayFromZero)返回不正确的值 - Using Math.Round(ReportItems!category.Value,MidpointRounding.AwayFromZero) returns incorrect value 如何使用NET Math.Round( <decimal> , <int> ,MidpointRounding.AwayFromZero) - How to round off correctly with NET Math.Round(<decimal>,<int>,MidpointRounding.AwayFromZero) .NET Math.Round(<double>,<int>,MidpointRounding.AwayFromZero)无法正常工作 - .NET Math.Round(<double>,<int>,MidpointRounding.AwayFromZero) not working correctly 为什么Math.Round(106.8,2,MidpointRounding.AwayFromZero)返回106.8而不是106.80? - Why does Math.Round(106.8, 2, MidpointRounding.AwayFromZero) return 106.8 and not 106.80? Math.Round在AwayFromZero模式下失败 - Math.Round fail on AwayFromZero mode 中点舍入为零 - MidpointRounding.AwayFromZero 指定小数位数时,Math.Round与MidpointRounding.ToEven的行为是否正确? - When specifying the number of decimals, is the behaviour of Math.Round with MidpointRounding.ToEven correct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM