Skip to content

Instantly share code, notes, and snippets.

@dchw
Created April 12, 2014 08:55
Show Gist options
  • Save dchw/10525586 to your computer and use it in GitHub Desktop.
Save dchw/10525586 to your computer and use it in GitHub Desktop.
Different types of rounding and casting, and how they affect decimals.
var numsToRound = new []{0.5, 1.5, 2.5, 3.5, 4.5};
Console.WriteLine("Banker|Away|Convert|Cast");
foreach(var num in numsToRound)
{
var toEven = Math.Round(num);
var awayFromZero = Math.Round(num, MidpointRounding.AwayFromZero);
var convert = Convert.ToInt32(num);
var cast = (int)num;
Console.WriteLine(String.Format(" {0} | {1} | {2} | {3} ", toEven, awayFromZero, convert, cast));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment