How to round a integer to the close hundred?

前端 未结 9 1190
生来不讨喜
生来不讨喜 2020-11-29 08:57

I don\'t know it my nomenclature is correct! Anyway, these are the integer I have, for example :

76
121
9660

And I\'d like to round them to

相关标签:
9条回答
  • 2020-11-29 09:35

    Hi i write this extension this gets the next hundred for each number you pass

    /// <summary>
        /// this extension gets the next hunfìdred for any number you whant
        /// </summary>
        /// <param name="i">numeber to rounded</param>
        /// <returns>the next hundred number</returns>
        /// <remarks>
        /// eg.:
        /// i =   21 gets 100
        /// i =  121 gets 200
        /// i =  200 gets 300
        /// i = 1211 gets 1300
        /// i = -108 gets -200
        /// </remarks>
        public static int RoundToNextHundred(this int i)
        {
            return i += (100 * Math.Sign(i) - i % 100);
            //use this line below if you want RoundHundred not NEXT
            //return i % 100 == byte.MinValue? i : i += (100 * Math.Sign(i) - i % 100);
        }
    
        //and for answer at title point use this algoritm
        var closeHundred = Math.Round(number / 100D)*100;
    
        //and here the extension method if you prefer
    
        /// <summary>
        /// this extension gets the close hundred for any number you whant
        /// </summary>
        /// <param name="number">number to be rounded</param>
        /// <returns>the close hundred number</returns>
        /// <remarks>
        /// eg.:
        /// number =   21 gets    0
        /// number =  149 gets  100
        /// number =  151 gets  200
        /// number = -149 gets -100
        /// number = -151 gets -200
        /// </remarks>
        public static int RoundCloseHundred(this int number)
        {
            return (int)Math.Round(number / 100D) * 100;
        }
    
    0 讨论(0)
  • 2020-11-29 09:45

    I had a similar project internally where the business requirements were to search within the 100's range of a given number and find duplicate DB records. So if the user was using line 856 I would search 800 - 899. If the user was using 8567 I would search 8500 - 8599. Not an exact rounding by 100's, but thought I would include my unique approach as some of these basic coding questions are embedded within a larger business project. To test this I seeded a decimal list from 1 - 99999 and spit the results out into a file.

        /// <summary>
        /// This method accepts an inbound Line Number and returns the line range
        /// in the form of lowest to highest based on 100's
        /// Example would be 9122 returns 9100 - 9199
        /// It's used for generating some additional BOM Temp functionality.
        /// </summary>
        /// <param name="inboundNumber"></param>
        /// <returns></returns>
        public static ProjectLineRange CalculateLineRange(decimal inboundNumber)
        {
            var lineRange = new ProjectLineRange();
            var numberLength = inboundNumber.ToString(CultureInfo.InvariantCulture).Length;
    
            switch (numberLength)
            {
                case 0: //NULL?
                    break;
                case 1: //Represents 1 - 9
                    lineRange.LineBottom = 1;
                    lineRange.LineTop = 99;
                    break;
                case 2: //Represents 10 - 99
                    lineRange.LineBottom = 1;
                    lineRange.LineTop = 99;
                    break;
                case 3: //Represents 100 - 999
                    lineRange = CalculateHundredsRange((int)(inboundNumber / 100));
                    break;
                case 4: //Represents 1000 - 9999
                    lineRange = CalculateThousandsRange(
                        Convert.ToInt32(inboundNumber.ToString(CultureInfo.InvariantCulture).Substring(1, 1)),
                        Convert.ToInt32(inboundNumber.ToString(CultureInfo.InvariantCulture).Substring(0, 1)));
                    break;
                case 5: //Represents 10000 - 99999
                    lineRange = CalculateTenThousandsRange(
                        Convert.ToInt32(inboundNumber.ToString(CultureInfo.InvariantCulture).Substring(2, 1)),
                        Convert.ToInt32(inboundNumber.ToString(CultureInfo.InvariantCulture).Substring(1, 1)),
                        Convert.ToInt32(inboundNumber.ToString(CultureInfo.InvariantCulture).Substring(0, 1)));
                    break;
            }
    
            return lineRange;
        }
    
    public class ProjectLineRange
        {
           public decimal LineBottom { get; set; }
           public decimal LineTop { get; set; }
        }
    
        /// <summary>
        /// Helper method to return the range for the 100's place
        /// </summary>
        /// <param name="hundredsPlaceValue"></param>
        /// <returns></returns>
        public static ProjectLineRange CalculateHundredsRange(int hundredsPlaceValue)
        {
            var tempLineRange = new ProjectLineRange();
            tempLineRange.LineBottom = hundredsPlaceValue * 100;
            tempLineRange.LineTop = tempLineRange.LineBottom + 99;
    
            return tempLineRange;
        }
    
        /// <summary>
        /// Helper method to return the range for the 100's place when factoring a 1000's number
        /// </summary>
        /// <param name="hundredsPlaceValue"></param>
        /// <param name="thousandsPlaceValue"></param>
        /// <returns></returns>
        public static ProjectLineRange CalculateThousandsRange(int hundredsPlaceValue, int thousandsPlaceValue)
        {
            var tempLineRange = new ProjectLineRange();
            var tempThousands = thousandsPlaceValue * 1000;
            var hundredsRange = CalculateHundredsRange(hundredsPlaceValue);
            tempLineRange.LineBottom = tempThousands + hundredsRange.LineBottom;
            tempLineRange.LineTop = tempThousands + hundredsRange.LineTop;
    
            return tempLineRange;
        }
    
        /// <summary>
        /// Helper method to return the range for the 100's place when factoring a 10000's number
        /// </summary>
        /// <param name="hundredsPlaceValue"></param>
        /// <param name="thousandsPlaceValue"></param>
        /// <param name="tenThousandsPlaceValue"></param>
        /// <returns></returns>
        public static ProjectLineRange CalculateTenThousandsRange(int hundredsPlaceValue, int thousandsPlaceValue, int tenThousandsPlaceValue)
        {
            var tempLineRange = new ProjectLineRange();
            var tempThousands = thousandsPlaceValue * 1000;
            var tempTenThousands = tenThousandsPlaceValue * 10000;
            var hundredsRange = CalculateHundredsRange(hundredsPlaceValue);
            tempLineRange.LineBottom = tempTenThousands + tempThousands + hundredsRange.LineBottom;
            tempLineRange.LineTop = tempTenThousands + tempThousands + hundredsRange.LineTop;
    
            return tempLineRange;
        }
    
    0 讨论(0)
  • 2020-11-29 09:49

    I wrote a simple extension method to generalize this kind of rounding a while ago:

    public static class MathExtensions
    {
        public static int Round(this int i, int nearest)
        {
            if (nearest <= 0 || nearest % 10 != 0)
                throw new ArgumentOutOfRangeException("nearest", "Must round to a positive multiple of 10");
    
            return (i + 5 * nearest / 10) / nearest * nearest;
        }
    }
    

    It leverages integer division to find the closest rounding.

    Example use:

    int example = 152;
    Console.WriteLine(example.Round(100)); // round to the nearest 100
    Console.WriteLine(example.Round(10)); // round to the nearest 10
    

    And in your example:

    Console.WriteLine(76.Round(100)); // 100
    Console.WriteLine(121.Round(100)); // 100
    Console.WriteLine(9660.Round(100)); // 9700
    
    0 讨论(0)
提交回复
热议问题