当值大于2.5时,舍入一个int? [英] Round up an int when value is more than 2.5?

查看:66
本文介绍了当值大于2.5时,舍入一个int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我在google上搜索了这个,但没有任何帮助我,所以有人知道我如何从例2.5中收集一个值 - > 3.0(带整数)。在我的程序中,我有一个下降率(从老板那里得到一个下降是多么常见),它可以说速率是1:6但实际上如果我计算它,它是1:6.6或者其他什么,那么我想要它反而说1:7但是我怎么能这样做?



我现在正在使用这个:



Hi everyone! I've searched on google for this but nothing has helped me so does anyone know how i can round up a value from example 2.5 -> 3.0 (with integers). In my program i have a drop rate (how common it is to get a drop from a boss) and it can say that the rate is 1:6 but actually if i calculate it, it's 1:6.6 or something, then i want it to say 1:7 instead but how can i do this?

I'm using this right now:

int ratecalculation = dropNumber / (int) total; //dropNumber keeps the value for all my items in the listbox, total is every items that get selected when i search for something
                    rate.Text = "Drop rate: 1:" + decimal.Round(ratecalculation, MidpointRounding.ToEven);

推荐答案

首先你需要做一个真正的除法,而不是整数除法(当分子和分母都是整数时,你做一个整数除法。)

然后你可以应用Round()函数。

这样:

First you need to do a REAL division, not an integer one (you do an integer division when numerator and denominator both are integers).
Then you can apply the Round() function.
This way:
float ratecalculation = (float)dropNumber / total;
int roundedCalculation = (int)Math.Round(ratecalculation);





希望这会有所帮助。



Hope this helps.


// ***************************************************** round

// http://en.wikipedia.org/wiki/Rounding

static int round ( float parameter )
    {

    return ( ( int ) ( parameter + 0.5F ) );
    }

// ***************************************************** round

// http://en.wikipedia.org/wiki/Rounding

static int round ( double parameter )
    {

    return ( ( int ) ( parameter + 0.5 ) );
    }


这篇关于当值大于2.5时,舍入一个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆