Esempio n. 1
0
 public RoundingPolicy(RoundingStrategy s = RoundingStrategy.Round, sbyte decimals = 2,
                       byte digit         = 5, double step = 0.1)
 {
     Strategy      = s;
     this.decimals = validateDecimals(decimals);
     this.digit    = validateDigit(digit);
     this.step     = validateStep(step);
 }
Esempio n. 2
0
 public RoundingPolicy(RoundingStrategy roundingStrategy, sbyte numberOfDigits) : this()
 {
     if (isRoundByStep(roundingStrategy))
     {
         return;
     }
     RoundingStrategy = roundingStrategy;
     NumberOfDigits   = numberOfDigits;
 }
Esempio n. 3
0
 public RoundingPolicy(RoundingStrategy roundingStrategy, double roundingStep) : this()
 {
     if (!isRoundByStep(roundingStrategy))
     {
         return;
     }
     RoundingStrategy = roundingStrategy;
     RoundingStep     = roundingStep;
 }
Esempio n. 4
0
 public RoundingPolicy(RoundingStrategy roundingStrategy, sbyte numberOfDigits, byte roundingDigit) : this()
 {
     if (roundingStrategy != RoundingStrategy.Round)
     {
         return;
     }
     RoundingStrategy = roundingStrategy;
     NumberOfDigits   = numberOfDigits;
     RoundingDigit    = roundingDigit;
 }
Esempio n. 5
0
 private static void testRoundingPolicy(RoundingPolicy o,
                                        RoundingStrategy roundingStrategy = RoundingStrategy.Round,
                                        sbyte numberOfDigits = 2,
                                        byte roundingDigit   = 5,
                                        double roundingStep  = 0)
 {
     Assert.AreEqual(roundingStrategy, o.RoundingStrategy);
     Assert.AreEqual(numberOfDigits, o.NumberOfDigits);
     Assert.AreEqual(roundingDigit, o.RoundingDigit);
     Assert.AreEqual(roundingStep, o.RoundingStep);
 }
 //TODO: this constructor is ONLY for unit tests - replace with a builder
 public FundingStreamPeriodProfilePattern(string fundingPeriodId,
                                          string fundingStreamId,
                                          string fundingLineId,
                                          DateTime fundingStreamPeriodStartDate,
                                          DateTime fundingStreamPeriodEndDate,
                                          bool allowUserToEditProfilePattern,
                                          ProfilePeriodPattern[] profilePattern,
                                          string profilePatternDisplayName,
                                          string profilePatternDescription,
                                          RoundingStrategy roundingStrategy)
 {
     FundingPeriodId = fundingPeriodId;
     FundingStreamId = fundingStreamId;
     FundingLineId   = fundingLineId;
     FundingStreamPeriodStartDate  = fundingStreamPeriodStartDate;
     FundingStreamPeriodEndDate    = fundingStreamPeriodEndDate;
     AllowUserToEditProfilePattern = allowUserToEditProfilePattern;
     ProfilePattern            = profilePattern;
     ProfilePatternDisplayName = profilePatternDisplayName;
     ProfilePatternDescription = profilePatternDescription;
     RoundingStrategy          = roundingStrategy;
 }
Esempio n. 7
0
 internal static bool isRoundByStep(RoundingStrategy roundingStrategy)
 {
     return(roundingStrategy == RoundingStrategy.RoundDownByStep ||
            roundingStrategy == RoundingStrategy.RoundUpByStep);
 }
Esempio n. 8
0
        private IReadOnlyCollection <DeliveryProfilePeriod> ApplyProfilePattern(
            decimal fundingValue,
            IReadOnlyCollection <ProfilePeriodPattern> profilePattern,
            IReadOnlyCollection <DeliveryProfilePeriod> profilePeriods,
            RoundingStrategy roundingStrategy)
        {
            List <DeliveryProfilePeriod> calculatedDeliveryProfile = new List <DeliveryProfilePeriod>();

            if (profilePeriods.Any())
            {
                decimal allocationValueToBeProfiled = Convert.ToDecimal(fundingValue);
                decimal runningTotal = 0;

                List <DeliveryProfilePeriod> profiledValues = profilePeriods.Select(pp =>
                {
                    ProfilePeriodPattern profilePeriodPattern = profilePattern.Single(
                        pattern => string.Equals(pattern.Period, pp.TypeValue) &&
                        string.Equals(pattern.DistributionPeriod, pp.DistributionPeriod) &&
                        pattern.PeriodYear == pp.Year &&
                        pattern.Occurrence == pp.Occurrence);

                    decimal profilePercentage = profilePeriodPattern
                                                .PeriodPatternPercentage;

                    decimal profiledValue = profilePercentage * allocationValueToBeProfiled;
                    if (profiledValue != 0)
                    {
                        profiledValue /= 100;
                    }

                    decimal roundedValue;


                    if (roundingStrategy == RoundingStrategy.RoundUp)
                    {
                        roundedValue = profiledValue
                                       .RoundToDecimalPlaces(2)
                                       .RoundToDecimalPlaces(0);
                    }
                    else
                    {
                        roundedValue = (int)profiledValue;
                    }

                    if (runningTotal + roundedValue > allocationValueToBeProfiled)
                    {
                        roundedValue = allocationValueToBeProfiled - runningTotal;
                    }

                    runningTotal += roundedValue;

                    return(pp.WithValue(roundedValue));
                }).ToList();

                DeliveryProfilePeriod last = profiledValues.Last();

                IEnumerable <DeliveryProfilePeriod> withoutLast = profiledValues.Take(profiledValues.Count - 1).ToList();

                calculatedDeliveryProfile.AddRange(
                    withoutLast.Append(
                        last.WithValue(allocationValueToBeProfiled - withoutLast.Sum(cdp => cdp.ProfileValue))));
            }

            return(calculatedDeliveryProfile);
        }
Esempio n. 9
0
        private IReadOnlyCollection <DeliveryProfilePeriod> GetProfiledAllocationPeriodsWithPatternApplied(decimal fundingValue,
                                                                                                           IReadOnlyCollection <ProfilePeriodPattern> profilePattern,
                                                                                                           RoundingStrategy roundingStrategy)
        {
            IReadOnlyCollection <DeliveryProfilePeriod> allocationProfilePeriods =
                GetProfilePeriodsForAllocation(profilePattern);

            return(ApplyProfilePattern(fundingValue, profilePattern, allocationProfilePeriods, roundingStrategy));
        }
        public static Money Round(this Money money, RoundingMode roundingMode, DecimalPlaces decimalPlaces)
        {
            RoundingStrategy strategy = RoundingStrategyFactory.GetRoundingStrategy(roundingMode);

            return(strategy.Round(money, (int)decimalPlaces));
        }