Extensions/DecimalExtensions.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;

namespace ComLib.Extensions
{

    /// <summary>
    /// Collection of utility extension methods related to the decimal data
    /// type.
    /// </summary>
    public static class DecimalExtensions
    {
        /// <summary>
        /// Method that finds a digit at an arbirary position of a decimal.
        /// <param name="number">The number.</param>
        /// <param name="position">The position of the digit to the right of the decimal (1-n).</param>
        /// <returns>Digit at position</returns>
        /// <example>
        /// var number = 1.2459m;
        /// var digit = number.DigitAtPosition(3); // value is 5
        /// </example>
        /// <remarks>See also http://stackoverflow.com/questions/2923510/what-is-the-best-way-to-find-the-digit-at-n-position-in-a-decimal-number/2924042#2924042. </remarks>
        /// </summary>
        public static int DigitAtPosition(this decimal number, int position)
        {
            if (position <= 0)
            {
                throw new ArgumentException("Position must be positive.");
            }

            if (number < 0)
            {
                number = Math.Abs(number);
            }

            return number.SanitizedDigitAtPosition(position);
        }

        private static int SanitizedDigitAtPosition(this decimal sanitizedNumber,
            int validPosition)
        {
            sanitizedNumber -= Math.Floor(sanitizedNumber);

            if (sanitizedNumber == 0)
            {
                return 0;
            }

            if (validPosition == 1)
            {
                return (int)(sanitizedNumber * 10);
            }

            return (sanitizedNumber * 10).SanitizedDigitAtPosition(validPosition - 1);
        }

        /// <summary>
        /// Returns a specified decimal number to a specified power. This method
        /// performs slower than the Math.Pow() method, but in some circumstances
        /// it is more accurate.
        /// </summary>
后面还有 60 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。