Forum C# Rounding in C#

Rounding in C#

Postby mfranzs » June 2nd, 2011, 5:04 am

Hey-

I noticed that AS3 and C# round numbers differently.

When using Math.Round in c# on 0.5, it returns 0.

When I use Math.round in AS3 it returns, 1.

It seems to me that AS3 rounds UP 0.5 to 1, but c# rounds 0.5 TOWARDS ZERO to 0.
(AS3 rounded -0.5 to 0 and C# rounded -0.5 to 0 as well);


How can I make C# always round up, or at least make the two act the same?

Thanks!
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Rounding in C#

Postby Benjaminsen » June 2nd, 2011, 9:56 am

Math.Round in C# can be parameterized to act pretty much as you want it to.

More info here: http://www.dotnetperls.com/math-round

Alternatively you could just floor or ceil the number.
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Rounding in C#

Postby mfranzs » June 2nd, 2011, 10:15 pm

Ugh, I just gave up and used a custom class :P

Code: Select all
public double Round(double value)
{
       return Math.Floor(value + 0.5);
}


Thanks for the help though! :D
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Rounding in C#

Postby mfranzs » June 2nd, 2011, 11:33 pm

Okay, ignore that :P

Do you guys know a way to use bankers rounding / round to evens in as3?
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Rounding in C#

Postby mfranzs » June 3rd, 2011, 12:11 am

Okay... here's what I came up with
Code: Select all
function bankround(r){
   var n=Math.round(r);
   var z=r%1
   if(z==0.5||z==-0.5){
      var x=n%2
      if(x==1||x==-1){
         n--;
      }
   }
   return n;            
}


If you know something more efficient, please let me know ^_^
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Rounding in C#

Postby siteadmin » June 5th, 2011, 3:00 am

Code: Select all
// Uses an enum to determine rounding method
public int Round(int num, RoundMethod roundmethod = RoundMethod.AwayZero)
{
    if (int % 1 == 0)
    {
        return int;
    }
    else if (int % 1 < 0.5)
    {
        return Math.Floor(int);
    }
    else if (int % 1 > 0.5)
    {
        return Math.Ceiling(int);
    }
    else
    {
        switch(roundmethod)
        {
            case RoundMethod.Down:
                return int - 0.5; break;
            case RoundMethod.Up:
                return int + 0.5; break;
            case RoundMethod.ToZero:
                return int - 0.5 * Math.Sign(int); break;
            case RoundMethod.AwayZero:
                return int + 0.5 * Math.Sign(int); break;
            case RoundMethod.ToEven:
                return int + (int % 2 - 1); break;
            case RoundMethod.ToOdd:
                return int - (int % 2 - 1); break;
        }
    }
}
Last edited by siteadmin on June 5th, 2011, 3:04 am, edited 2 times in total.
siteadmin
 
Posts: 2
Joined: June 5th, 2011, 2:44 am

Re: Rounding in C#

Postby mfranzs » June 5th, 2011, 3:01 am

... Uhm....

What does that code do? :P
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Rounding in C#

Postby siteadmin » June 5th, 2011, 3:05 am

It uses an enum called RoundMethod to determine rounding behavior.
siteadmin
 
Posts: 2
Joined: June 5th, 2011, 2:44 am

Re: Rounding in C#

Postby mfranzs » June 5th, 2011, 3:40 am

Oh, okay... thanks!
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am


Return to C#



cron