public static double RoundUp(double v, double amount) { const System.MidpointRounding rounding = System.MidpointRounding.ToEven; var result = Round(v + (amount / 2.0), rounding, amount); return(result); }
/// <summary> /// rounds val to the nearest fractional value /// </summary> /// <param name="val">the value tp round</param> /// <param name="rounding">what kind of rounding</param> /// <param name="frac"> round to this value (must be greater than 0.0)</param> /// <returns>the rounded value</returns> private double Round(double val, System.MidpointRounding rounding, double frac) { if (frac <= 0) { throw new System.ArgumentOutOfRangeException(nameof(frac), "must be greater than or equal to 0.0"); } double retval = System.Math.Round((val / frac), rounding) * frac; return(retval); }
/// <summary> /// rounds val to the nearest fractional value /// </summary> /// <param name="val">the value tp round</param> /// <param name="rounding">what kind of rounding</param> /// <param name="frac"> round to this value (must be greater than 0.0)</param> /// <returns>the rounded value</returns> public static double Round(double val, System.MidpointRounding rounding, double frac) { /* * if (frac <= 0) * { * throw new ArgumentOutOfRangeException("frac","must be greater than or equal to 0.0"); * }*/ double retval = System.Math.Round((val / frac), rounding) * frac; return(retval); }
public static double RoundApproximate(double dbl, int digits, double margin, System.MidpointRounding mode) { double fraction = dbl * System.Math.Pow(10, digits); double value = System.Math.Truncate(fraction); fraction = fraction - value; if (fraction == 0) { return(dbl); } double tolerance = margin * dbl; // Determine whether this is a midpoint value. if ((fraction >= .5 - tolerance) & (fraction <= .5 + tolerance)) { if (mode == System.MidpointRounding.AwayFromZero) { return((value + 1) / System.Math.Pow(10, digits)); } else if (value % 2 != 0) { return((value + 1) / System.Math.Pow(10, digits)); } else { return(value / System.Math.Pow(10, digits)); } } // Any remaining fractional value greater than .5 is not a midpoint value. if (fraction > .5) { return((value + 1) / System.Math.Pow(10, digits)); } else { return(value / System.Math.Pow(10, digits)); } }
/// <summary> /// <see cref="System.Math.Round(decimal, int, System.MidpointRounding)"/> のラップメソッドを提供します。 /// </summary> /// <param name="value">インスタンス。</param> /// <param name="digits">小数部桁数。</param> /// <param name="mode">丸めの方法。</param> /// <returns> /// 丸めた結果を返却します。 /// </returns> public static decimal Round(this decimal value, int digits = 0, System.MidpointRounding mode = System.MidpointRounding.ToEven) { return(System.Math.Round(value, digits, mode)); }
public static float RoundBelowZero(this float v, int d = 7, System.MidpointRounding r = default(System.MidpointRounding)) { return((float)System.Math.Round(v, d, r)); }
public static Size Round(Size value, System.MidpointRounding midpointRounding = System.MidpointRounding.ToEven) { return(new Size(Round(value.Width, midpointRounding), Round(value.Height, midpointRounding))); }
public static Vector2 Round(Vector2 value, System.MidpointRounding midpointRounding = System.MidpointRounding.ToEven) { return(new Vector2(Round(value.X, midpointRounding), Round(value.Y, midpointRounding))); }
public static double Round(double n, System.MidpointRounding midpointRounding = System.MidpointRounding.ToEven) { return(System.Math.Round(n, midpointRounding)); }
public static float Round(float n, System.MidpointRounding midpointRounding = System.MidpointRounding.ToEven) { return((float)System.Math.Round(n, midpointRounding)); }
/// <summary> /// Get the value of <see cref="System.Math.Round(System.Decimal, System.Int32, System.MidpointRounding)"/> for this <see cref="System.Decimal"/> value. /// </summary> public static System.Decimal RoundExt(this System.Decimal d, System.Int32 decimals, System.MidpointRounding mode) { return(System.Math.Round(d, decimals, mode)); }
/// <summary> /// Get the value of <see cref="System.Math.Round(System.Double, System.Int32, System.MidpointRounding)"/> for this <see cref="System.Double"/> value. /// </summary> public static System.Double Round(this System.Double value, System.Int32 digits, System.MidpointRounding mode) { return(System.Math.Round(value, digits, mode)); }
/// <inheritdoc cref="System.Math.Round(System.Decimal, System.MidpointRounding)" /> public static System.Decimal Round(this System.Decimal d, System.MidpointRounding mode) { return(System.Math.Round(d, mode)); }