/// <summary> /// Rounds the value specific to the wanted <see cref="RoundingType"/>. /// </summary> /// <param name="value">The value to round.</param> /// <param name="result">The rounded result value.</param> /// <param name="howToRound">How to round the value?</param> /// <param name="checkedCalculation">If true all possible exceptions are enabled.</param> /// <returns>The rounded value.</returns> /// <exception cref="ArgumentOutOfRangeException">The value to round is <see cref="double.NaN"/>!</exception> /// <exception cref="ArgumentOutOfRangeException">The value is not in the range of a <see cref="int"/>!</exception> /// <exception cref="ArgumentOutOfRangeException">The given <see cref="RoundingType"/> is not a valid <see cref="RoundingType"/>!</exception> public static void RoundSpecific(double value, out int result, RoundingType howToRound = RoundingType.Truncate, bool checkedCalculation = false) { if (checkedCalculation) { if (double.IsNaN(value)) { throw new ArgumentOutOfRangeException("The value to round is Not a Number!"); } if (value < int.MinValue || value > int.MaxValue) { throw new ArgumentOutOfRangeException("The value is not in the range of a byte!"); } } double tempResult = FloatExtensions.RoundSpecific(value, howToRound); if (double.IsNaN(tempResult) || tempResult > int.MaxValue) { result = int.MaxValue; } else if (tempResult < int.MinValue) { result = int.MinValue; } else { result = (int)tempResult; } }
/// <summary> /// Rounds the value specific to the wanted <see cref="RoundingType"/>. /// </summary> /// <param name="value">The value to round.</param> /// <param name="result">The rounded result value.</param> /// <param name="howToRound">How to round the value?</param> /// <param name="checkedCalculation">If true all possible exceptions are enabled.</param> /// <returns>The rounded value.</returns> /// <exception cref="ArgumentOutOfRangeException">The value is not in the range of a <see cref="byte"/>!</exception> /// <exception cref="ArgumentOutOfRangeException">The given <see cref="RoundingType"/> is not a valid <see cref="RoundingType"/>!</exception> public static void RoundSpecific(decimal value, out byte result, RoundingType howToRound = RoundingType.Truncate, bool checkedCalculation = false) { if (checkedCalculation) { if (value < byte.MinValue || value > byte.MaxValue) { throw new ArgumentOutOfRangeException("The value is not in the range of a byte!"); } } decimal tempResult = FloatExtensions.RoundSpecific(value, howToRound); if (tempResult > byte.MaxValue) { result = byte.MaxValue; } else if (tempResult < byte.MinValue) { result = byte.MinValue; } else { result = (byte)tempResult; } }