Created
May 22, 2019 13:37
-
-
Save sahgilbert/dce7c94941aa348dcb5c4131443b4661 to your computer and use it in GitHub Desktop.
Numerical Extension Methods - C# .Net Core
This file contains 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
using System; | |
namespace SimonGilbert.Blog | |
{ | |
public static class NumericalExtensions | |
{ | |
public static double RoundByDecimalPlaces(this double number, int numberOfDecimalPlaces = 2) | |
{ | |
return Math.Round(number, numberOfDecimalPlaces); | |
} | |
public static double RoundUpOrDown(this double number) | |
{ | |
return PerformRoundedDivision(number); | |
} | |
private static double PerformRoundedDivision(double number) | |
{ | |
double divisor = 1; | |
double div = number / divisor; | |
double floor = Math.Floor(div); | |
double celing = Math.Ceiling(div); | |
var difference = (div - floor); | |
return difference < 0.5 ? floor : celing; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment