将int除以10舍入为最接近的int(Rounding up an int divided by 10 to nearest int)
我想将int除以10得到一个最接近舍入的整数,所以12应该给1,17应该给2
继承我的代码:
int BonusValue; int Str; BonusValue = Convert.ToInt32(Math.Round(Convert.ToDouble(Str) / 10));这会有用吗?
I want to divide an int by 10 and get a whole number closest to rounded one, so 12 should give 1 and 17 should give 2
Heres my code:
int BonusValue; int Str; BonusValue = Convert.ToInt32(Math.Round(Convert.ToDouble(Str) / 10));Will this work?
最满意答案
是的,那应该有用。
但是,如果将整数中的一半舍入步骤(在您的情况下为5)添加,则会更容易。 然后你可以做简单的整数除法:
BonusValue = (Str+5) / 10
Yes, that should work.
It is however much easier if you add half the rounding step, in your case 5, to the integer. Then you can do simple integer division:
BonusValue = (Str+5) / 10
更多推荐
发布评论