Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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