[英]VB.NET How to use the Math.Round() function to round the percentage to a whole number
Noob here so apologies in advance if this is not worded correctly. 如果没有正确用词,Noob会在此提前致歉。
I have a function which is calculating two Integer variables to work out a percentage. 我有一个函数,用于计算两个Integer变量以得出百分比。
Public Function CalculatePercentCorrect() As Double
Return (Correct / Tries) * 100
End Function
I am then using this property to display within a label 然后,我使用此属性在标签内显示
If game.CheckAnswer(answer) = True Then
AnswerLabel.Text = _
String.Format("Correct! Your score is {0} out of {1} for {2} percent.", _
game.Correct, game.Tries, game.CalculatePercentCorrect())
Else
AnswerLabel.Text = _
String.Format("Incorrect. Your score is {0} out of {1} for {2} percent.", _
game.Correct, game.Tries, game.CalculatePercentCorrect())
End If
However I require the percentage to be rounded up to a whole number, as it may show 66.6666666666667 percent for example. 但是,我要求将百分比四舍五入为整数,例如,它可能显示为66.6666666666667%。
I believe I should use the Math.Round function, however I'm not entirely sure how to use it in my example. 我相信我应该使用Math.Round函数,但是我不确定在示例中如何使用它。
Any help would be much appreciated. 任何帮助将非常感激。
Math.Round Method on MSDN. MSDN上的Math.Round方法 。 Since it returns a Double when rounding a Double, for you it would be as simple as:
由于在对Double进行四舍五入时会返回Double,因此对您来说很简单:
String.Format("Correct! Your score is {0} out of {1} for {2} percent.", _
game.Correct, game.Tries, Math.Round(game.CalculatePercentCorrect()))
如果您想取整,可以使用
math.ceiling(game.CalculatePercentCorrect())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.