Click here to Skip to main content
16,003,319 members
Please Sign up or sign in to vote.
2.59/5 (3 votes)
See more:
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:

C#
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);
Posted
Updated 21-Mar-14 3:38am
v4

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:
C#
float ratecalculation = (float)dropNumber / total;
int roundedCalculation = (int)Math.Round(ratecalculation);


Hope this helps.
 
Share this answer
 
v3
Comments
Gramas19 21-Mar-14 9:47am    
Thank you very much, it works now! You're right, I must use a real divison to do that so i've learned something! :)
phil.o 21-Mar-14 9:49am    
You're welcome. Glad to have been of any help :)
C#
// ***************************************************** 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 ) );
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900