将int除以10舍入为最接近的int(Rounding up an int divided by 10 to nearest int)

编程入门 行业动态 更新时间:2024-10-24 00:29:07
将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

更多推荐

本文发布于:2023-04-29 11:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最接近   int   nearest   divided   Rounding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!