示例#1
0
        /// <summary>
        /// Gets a <see cref="System.TimeSpan"/>
        /// </summary>
        /// <param name="span">The span to get the percentage of.</param>
        /// <param name="percentage">What percent of the full time should be returned.</param>
        /// <param name="roundTo">A timespan representing the nearast value the result should round to. Optional.</param>
        /// <returns>A timespan that is a percentage of the full time.</returns>
        /// <remarks>
        /// this is not just calling the <see cref="GetPercentage(STimeSpan, decimal, STimeSpan?)"/>
        /// because double and decimal have different precisions.
        /// </remarks>
        public static STimeSpan GetPercentage(this STimeSpan span, double percentage, STimeSpan?roundTo = null)
        {
            long totalTicks = span.Ticks;
            var  result     = STimeSpan.FromTicks((long)(totalTicks * percentage));

            if (roundTo != null)
            {
                return(result.RoundToNearest(roundTo.Value));
            }

            return(result);
        }