3

I am trying to round my values so that if it's 0.5 or greater, it becomes 1, else it becomes 0. For example:

3.7 -> 4;
1.3 -> 1;
2.5 -> 3;
...

Any ideas?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • 2
    does Math.Round not work or are there specific cases you haven't shown? – Jesus Ramos Oct 23 '11 at 04:49
  • Thanks, I tried it now, it seems to work. I was trying the approach suggested here, that's why I thought I needed something else. If you want, post it as an answer, I will mark it as an answer. – Joan Venge Oct 23 '11 at 04:53
  • You can just accept manojlds since he already posted the same thing I would post. – Jesus Ramos Oct 23 '11 at 04:54
  • 1
    Everything you need to know about C# rounding can be found at http://stackoverflow.com/questions/14/whats-the-difference-between-math-floor-and-math-truncate-in-net/580252#580252 – paxdiablo Oct 23 '11 at 05:12

4 Answers4

8
Math.Round(3.7,MidpointRounding.AwayFromZero);

http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

In the above, I made use of AwayFromZero for rounding because the default is Banker's rounding, so if the fraction is 0.5, it is rounded to nearest even. So 3.5 becomes 4 (nearest even), but 2.5 becomes 2 (nearest even). So you choose a different method as shown above to make 3.5 to 4 and 2.5 to 3.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Thanks, why 3.5 becomes 4 but 2.5 becomes 2? Should I use AwayFromZero like you showed? I am reading the link you posted now. – Joan Venge Oct 23 '11 at 05:02
  • @JoanVenge - Like I said, the default is Banker's rounding and that is how it works. It rounds the 0.5 towards the nearest **even**. So 3.5 becomes 4 ( nearest even), and 2.5 becomes 2 ( nearest even). This is the default. So you will have to use AwayFromZero when you use Math.Round for what you want. – manojlds Oct 23 '11 at 05:05
3

The simplest way is add 0.5 to the input, then cast to int.

Phil Lello
  • 8,377
  • 2
  • 25
  • 34
2

I arrived last, so I'll tell something different. You round 0.5 to 1 by not using double! Use decimals. double aren't good to have "exact" numbers.

Launch this piece of code and have fun (note that there is/was a "bug" in mono on numbers like 0.49999999999999994, so to run it on ideone I had to modify it a little to try to round 1.5: http://ideone.com/57XAYV)

public static void Main()
{
    double d = 1.0;
    d -= 0.3;
    d -= 0.2;

    Console.WriteLine("Standard formatting: {0}", d); // 0.5
    Console.WriteLine("Internal Representation: {0:r}", d); // 0.49999999999999994
    Console.WriteLine("Console WriteLine 0 decimals: {0:0}", d); // 1
    Console.WriteLine("0 decimals Math.Round: {0}", Math.Round(d, MidpointRounding.AwayFromZero)); // 0
    Console.WriteLine("15 decimals then 0 decimals Math.Round: {0}", Math.Round(Math.Round(d, 15, MidpointRounding.AwayFromZero), MidpointRounding.AwayFromZero)); // 1
}
xanatos
  • 109,618
  • 12
  • 197
  • 280
1

Round-Up

Math.Round(3.5, 0, MidpointRounding.AwayFromZero) -> 4

Round Down

Math.Round(3.5, 0, MidpointRounding.ToEven) -> 3
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56