Skip to content

Instantly share code, notes, and snippets.

@sahgilbert
Created May 22, 2019 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahgilbert/dce7c94941aa348dcb5c4131443b4661 to your computer and use it in GitHub Desktop.
Save sahgilbert/dce7c94941aa348dcb5c4131443b4661 to your computer and use it in GitHub Desktop.
Numerical Extension Methods - C# .Net Core
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