/// <summary>
        /// Rounds values to nearest currency unit using the tender type rounding settings, i.e 16,45 kr. rounded up if the smallest coin is 10 kr will give 20 kr.
        /// or if the smallest coin is 24 aurar(0,25 kr.) then if rounded up it will give 16,50 kr.
        /// </summary>
        /// <param name="value">The currency value or value to be rounded.</param>
        /// <param name="unit">The smallest unit to be rounded to.</param>
        /// <param name="roundMethod">The method of rounding, None, Nearest, up or down</param>
        /// <returns>Returns a value rounded to the nearest unit.</returns>
        private decimal RoundToUnit(decimal value, decimal unit, TenderRoundMethod roundMethod)
        {
            if (roundMethod == TenderRoundMethod.None)
            {
                return(value);
            }

            /*
             * TenderRoundMethod = 0 - None, 1 - Nearest, 2 - Up, 3 - Down
             * RoundMethod = 0 - Nearest, 1 - Down, 2 - Up
             *
             * When rounding payment numbers then the TenderRoundingMethod should be but when
             * rounding tax or currency numbers then the RoundMethod should be used.
             *
             * If TenderRoundMethod is set to NONE then the value is returned without any rounding
             * otherwise we need to cast the TenderRoundingMethod to RoundingMethod and then call the original RoundToUnit function
             *
             */
            RoundMethod currencyRound = RoundMethod.RoundNearest;

            switch (roundMethod)
            {
            case TenderRoundMethod.RoundNearest: { currencyRound = RoundMethod.RoundNearest; break; }

            case TenderRoundMethod.RoundUp: { currencyRound = RoundMethod.RoundUp; break; }

            case TenderRoundMethod.RoundDown: { currencyRound = RoundMethod.RoundDown; break; }
            }
            return(RoundToUnit(value, unit, currencyRound));
        }
        /// <summary>
        /// Rounds values to nearest currency unit, i.e 16,45 kr. rounded up if the smallest coin is 10 kr will give 20 kr.
        /// or if the smallest coin is 24 aurar(0,25 kr.) then if rounded up it will give 16,50 kr.
        /// </summary>
        /// <param name="value">The currency value or value to be rounded.</param>
        /// <param name="unit">The smallest unit to be rounded to.</param>
        /// <param name="roundMethod">The method of rounding, Nearest,up and down</param>
        /// <returns>Returns a value rounded to the nearest unit.</returns>
        private decimal RoundToUnit(decimal value, decimal unit, RoundMethod roundMethod)
        {
            if (unit != 0)
            {
                number = 0;
                decimal decimalValue = value / unit;
                decimal difference   = Math.Abs(decimalValue) - Math.Abs(Math.Truncate(value / unit));

                // is rounding required?
                if (difference > 0)
                {
                    switch (roundMethod)
                    {
                    case RoundMethod.RoundNearest: { return(Math.Round(Math.Round((value / unit), 0, MidpointRounding.AwayFromZero) * unit, GetNumberOfDecimals(unit), MidpointRounding.AwayFromZero)); }

                    case RoundMethod.RoundDown:
                    {
                        if (value > 0M)
                        {
                            return(Math.Round(Math.Round((value / unit) - 0.5M, 0) * unit, GetNumberOfDecimals(unit)));
                        }
                        else
                        {
                            return(Math.Round(Math.Round((value / unit) + 0.5M, 0) * unit, GetNumberOfDecimals(unit)));
                        }
                    }

                    case RoundMethod.RoundUp:
                    {
                        if (value > 0M)
                        {
                            return(Math.Round(Math.Round((value / unit) + 0.5M, 0) * unit, GetNumberOfDecimals(unit)));
                        }
                        else
                        {
                            return(Math.Round(Math.Round((value / unit) - 0.5M, 0) * unit, GetNumberOfDecimals(unit)));
                        }
                    }
                    }
                }
            }
            return(value);
        }
Beispiel #3
0
        public DoubleCy Round100(RoundMethod method, double round, double delta, int baseRound)
        {
            if (method == RoundMethod.Progresywny)
            {
                return(this.Round100(round, delta));
            }
            if (method == RoundMethod.Prosty)
            {
                //return new DoubleCy(this.Round((int)round) - ((DoubleCy)delta));
                return(new DoubleCy(this.Round((int)round) - delta));
            }
            if (method != RoundMethod.WgCyfrZnaczacych)
            {
                return(new DoubleCy(this.value, this.Symbol));
            }
            string numberDecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;

            string[] strArray = Convert.ToString(Math.Round(this.value, baseRound)).Split(new string[] { numberDecimalSeparator }, StringSplitOptions.RemoveEmptyEntries);
            string   str2     = string.Empty;

            if (round <= strArray[0].Length)
            {
                int    length = strArray[0].Length;
                string s      = strArray[0].Substring(0, (int)round) + new string('0', wyliczDokładnośćDlaLiczbyCałkowitej(delta));
                delta = wyliczPomniejszenie(delta);
                double num2 = double.Parse(s) - delta;
                s    = Convert.ToString(num2);
                str2 = s + new string('0', length - s.Length);
            }
            else
            {
                round -= strArray[0].Length;
                string str4 = strArray[1].Substring(0, (int)round);
                str4 = (Parse(str4 + new string('0', strArray[1].Length - str4.Length)).Value - wyliczPomniejszenie(delta)).ToString();
                str2 = strArray[0] + numberDecimalSeparator + str4;
            }
            return(Parse(str2));
        }