Created
April 12, 2014 08:55
-
-
Save dchw/10525586 to your computer and use it in GitHub Desktop.
Different types of rounding and casting, and how they affect decimals.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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