int number = 236;
number = (int)(Math.Ceiling(number / 10.0d) * 10);
模(%)得到余数,所以你得到:
// number = 236 + 10 - 6
将其放入扩展方法中
public static int roundupbyten(this int i){
// return i + (10 - i % 10); <-- logic error. Oops!
return (int)(Math.Ceiling(i / 10.0d)*10); // fixed
}
// call like so:
int number = 236.roundupbyten();
public static class ExtensionMethods
{
public static int RoundOff (this int i)
{
return ((int)Math.Round(i / 10.0)) * 10;
}
}
int roundedNumber = 236.RoundOff(); // returns 240
int roundedNumber2 = 11.RoundOff(); // returns 10
public int RoundOff(int number, int interval){
int remainder = number % interval;
number += (remainder < interval / 2) ? -remainder : (interval - remainder);
return number;
}
使用方法:
int number = 11;
int roundednumber = RoundOff(number, 10);
public static int RoundToNearestMultipleOfFactor(this int value, int factor)
{
if (factor == 0)
{
throw new ArgumentOutOfRangeException(nameof(factor), factor, "Cannot be zero");
}
var halfAbsFactor = Math.Abs(factor) >> 1;
return value + Math.Sign(value) * (halfAbsFactor - (Math.Abs(value) % factor + halfAbsFactor % factor) % factor);
}
下面是整个扩展方法类,其中包含 int和 long的方法,以及只向零舍入或从零舍入的方法。
/// <summary>
/// Extension methods for rounding integral numeric types
/// </summary>
public static class IntegralRoundingExtensions
{
/// <summary>
/// Rounds to the nearest multiple of a <paramref name="factor"/> using <see cref="MidpointRounding.AwayFromZero"/> for midpoints.
/// <para>
/// Performs the operation Round(value / factor) * factor without converting to a floating type.
/// </para>
/// </summary>
/// <param name="value">The value to round.</param>
/// <param name="factor">The factor to round to a multiple of. Must not be zero. Sign does not matter.</param>
/// <remarks>
/// Uses math derived from the <see href="https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero">Round half away from zero equation</see>: y = sgn(x)*Floor(Abs(x) + 0.5) and floor equation: Floor(z) = z - (z % 1)
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="factor"/> is zero</exception>
/// <seealso cref="MidpointRounding"/>
public static long RoundToNearestMultipleOfFactor(this long value, long factor)
{
if (factor == 0)
{
throw new ArgumentOutOfRangeException(nameof(factor), factor, "Cannot be zero");
}
var halfAbsFactor = Math.Abs(factor) >> 1;
// return value + Math.Sign(value) * (halfAbsFactor - ((Math.Abs(value) + halfAbsFactor) % factor));
//fix overflow
return value + Math.Sign(value) * (halfAbsFactor - (Math.Abs(value) % factor + halfAbsFactor % factor) % factor);
}
/// <summary>
/// Round to the nearest multiple of <paramref name="factor"/> with magnitude less than or equal to <paramref name="value"/>.
/// </summary>
/// <param name="value">The value to round.</param>
/// <param name="factor">The factor to round to a multiple of. Must not be zero. Sign does not matter.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="factor"/> is zero</exception>
public static long RoundToMultipleOfFactorTowardZero(this long value, long factor)
{
if (factor == 0)
{
throw new ArgumentOutOfRangeException(nameof(factor), factor, "Cannot be zero");
}
var remainder = value % factor; // negative iff value is negative
if (remainder == 0)
{
return value;
}
return value - remainder;
}
/// <summary>
/// Round to the nearest multiple of <paramref name="factor"/> with magnitude greater than or equal to <paramref name="value"/>.
/// </summary>
/// <param name="value">The value to round.</param>
/// <param name="factor">The factor to round to a multiple of. Must not be zero. Sign does not matter.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="factor"/> is zero</exception>
public static long RoundToMultipleOfFactorAwayFromZero(this long value, long factor)
{
if (factor == 0)
{
throw new ArgumentOutOfRangeException(nameof(factor), factor, "Cannot be zero");
}
var remainder = value % factor; // negative iff value is negative
if (remainder == 0)
{
return value;
}
return value - remainder + Math.Sign(value) * Math.Abs(factor);
}
/// <summary>
/// Rounds to the nearest multiple of a <paramref name="factor"/> using <see cref="MidpointRounding.AwayFromZero"/> for midpoints.
/// <para>
/// Performs the operation Round(value / factor) * factor without converting to a floating type.
/// </para>
/// </summary>
/// <param name="value">The value to round.</param>
/// <param name="factor">The factor to round to a multiple of. Must not be zero. Sign does not matter.</param>
/// <remarks>
/// Uses math derived from the <see href="https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero">Round half away from zero equation</see>: y = sgn(x)*Floor(Abs(x) + 0.5) and floor equation: Floor(z) = z - (z % 1)
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="factor"/> is zero</exception>
/// <seealso cref="MidpointRounding"/>
public static int RoundToNearestMultipleOfFactor(this int value, int factor)
{
if (factor == 0)
{
throw new ArgumentOutOfRangeException(nameof(factor), factor, "Cannot be zero");
}
var halfAbsFactor = Math.Abs(factor) >> 1;
// return value + Math.Sign(value) * (halfAbsFactor - ((Math.Abs(value) + halfAbsFactor) % factor));
//fix overflow
return value + Math.Sign(value) * (halfAbsFactor - (Math.Abs(value) % factor + halfAbsFactor % factor) % factor);
}
/// <summary>
/// Round to the nearest multiple of <paramref name="factor"/> with magnitude less than or equal to <paramref name="value"/>.
/// </summary>
/// <param name="value">The value to round.</param>
/// <param name="factor">The factor to round to a multiple of. Must not be zero. Sign does not matter.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="factor"/> is zero</exception>
public static int RoundToMultipleOfFactorTowardZero(this int value, int factor)
{
if (factor == 0)
{
throw new ArgumentOutOfRangeException(nameof(factor), factor, "Cannot be zero");
}
var remainder = value % factor; // negative iff value is negative
if (remainder == 0)
{
return value;
}
return value - remainder;
}
/// <summary>
/// Round to the nearest multiple of <paramref name="factor"/> with magnitude greater than or equal to <paramref name="value"/>.
/// </summary>
/// <param name="value">The value to round.</param>
/// <param name="factor">The factor to round to a multiple of. Must not be zero. Sign does not matter.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="factor"/> is zero</exception>
public static int RoundToMultipleOfFactorAwayFromZero(this int value, int factor)
{
if (factor == 0)
{
throw new ArgumentOutOfRangeException(nameof(factor), factor, "Cannot be zero");
}
var remainder = value % factor; // negative iff value is negative
if (remainder == 0)
{
return value;
}
return value - remainder + Math.Sign(value) * Math.Abs(factor);
}
}
如果您正在使用 c # 8或更高版本,您可以创建小开关表达式实用程序方法,这些方法可以做很多很酷的有用的事情。如果您使用的是旧版本,可以使用 if/else和 switch case块:
public static int RoundIntToTens(int anInt)
=> (anInt, (anInt < 0 ? 0 - anInt : anInt) % 10) switch
{
// If int needs to be "round down" and is negative or positive
(>= 0, < 5) or (< 0, < 5) => anInt - anInt % 10,
// If int needs to be "round up" and is NOT negative (but might be 0)
(>= 0, >= 5) => anInt + (10 - anInt % 10),
// If int needs to be "round up" and is negative
(< 0, >= 5) => anInt - (10 + anInt % 10)
};