Results = (int)Math.Round(testCases, MidpointRounding.ToNegativeInfinity) This mode will round numbers down in the direction of negative infinity: public static int RoundDownUsingMathRoundWithToNegativeInfinityMode(double testCases) NET/C# content and get paid? > JOIN US! << MidpointRounding.ToNegativeInfinity Mode Wanna join Code Maze Team, help us produce more awesome. With this mode, desired results are only for positive values. Results are: Round down using Math.Round() with ToZero mode: Results = (int)Math.Round(testCases, MidpointRounding.ToZero) ToZero rounding mode will return values toward zero: public static int RoundDownUsingMathRoundWithToZeroMode(double testCases) Rounding -2,5 results in -3 (desired result: -3) MidpointRounding.ToZero Mode Rounding 2,5 results in 3 (desired result: 2) Results are: Round down using Math.Round() with AwayFromZero mode: Results = (int)Math.Round(testCases, MidpointRounding.AwayFromZero) When the number is halfway, this mode will round it to the nearest number away from zero: public static int RoundDownUsingMathRoundWithAwayFromZeroMode(double testCases) Rounding -2,5 results in -2 (desired result: -3) MidpointRounding.AwayFromZero ModeĪwayFromZero mode will round the number to the nearest integer. Rounding 1,5 results in 2 (desired result: 1) Rounding 1,75 results in 2 (desired result: 1) Here are the results: Round down using Math.Round() with ToEven mode: Results = (int)Math.Round(testCases, MidpointRounding.ToEven) This strategy is also known as banker’s rounding: public static int RoundDownUsingMathRoundWithToEvenMode(double testCases) When the number is halfway between two integers, this mode will round it to the nearest even number. The value of this parameter affects only results for numbers halfway between two integer values (values 2.5, 1.5, -1.5, and -2.5 from our example).NET defines five possible values for it, and we will explore each. Some have a MidpointRounding parameter that determines the rounding convention or modes. This method comes with several overloads. NET/C# content and get paid? > JOIN US! << It will return double, so we must cast the result to int. Math.Round() rounds the value to the nearest integral value or the provided number of fractional digits. Use Math.Round() Method to Round Down a Number to the Nearest Integer We can see that it returns the expected result only for positive numbers. ![]() Rounding -2,5 results in -2 (desired result: -3) Rounding -1,75 results in -1 (desired result: -2) Rounding -1,5 results in -1 (desired result: -2) Rounding -1,25 results in -1 (desired result: -2) The results of our testing are: Round down using Math.Truncate(): It returns double, so we need to cast results to an integer: public static int RoundDownUsingMathTruncate(double testCases)įor (var cnt = 0 cnt < testCases.Length cnt++) Math.Truncate() returns an integral part of a number. Math.Floor() is indeed returning values rounded down to the nearest integer. ![]() Rounding -2,5 results in -3 (desired result: -3) Rounding -1,75 results in -2 (desired result: -2) Rounding -1,5 results in -2 (desired result: -2) Rounding -1,25 results in -2 (desired result: -2) Rounding 1,25 results in 1 (desired result: 1) Rounding 1,5 results in 1 (desired result: 1) Rounding 1,75 results in 1 (desired result: 1) Rounding 2,5 results in 2 (desired result: 2)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |