Problem of the Day
A new programming or logic puzzle every Mon-Fri

Rounding

Let's do a rounding problem for problem #150. Today's objective is to implement your own rounding function (or method) for floating point numbers. That means not using the built in rounding methods of the language of your choice. Your function should have a flag to round down, round normally (at .5), and round up.

Permalink: http://problemotd.com/problem/rounding/

Comments:

  • Anonymous - 10 years, 2 months ago

    #python 2.7
    
    def rounding(x):
        flag = 0.5
        x = float(x)
        y = int(x)
        z = x - y
        if z >= flag:
            a = y + 1
        else:
            a = y
    
        print a
        return a
    
    r = float(raw_input("Enter a number> "))
    rounding(r)
    

    reply permalink

  • Oscar - 10 years, 2 months ago

    FLAG_ROUNDING = Enum('FLAG_ROUNDING', 'DOWN NORMAL UP')
    
    def custom_round(n, flag=FLAG_ROUNDING.NORMAL):
        truncated = int(n)
        if flag == FLAG_ROUNDING.DOWN:
            return truncated
        elif flag == FLAG_ROUNDING.UP:
            return truncated + 1
        elif flag == FLAG_ROUNDING.NORMAL:
            return truncated if n - truncated < 0.5 else truncated + 1
    

    I'm not aware of a better way of truncating the number than just converting it to an int, and I'm not sure if it's allowed in the exercise. Feedback appreciated.

    reply permalink

  • Ben - 10 years, 2 months ago

    C#

        public enum RoundingOption
        {
             RoundDown,
             RoundUp,
             Normal
        }
    
        public Decimal Round(Decimal x, RoundingOption option)
        {
            int intx= (int)x;
            Decimal retval;
    
            switch(option)
            {
                case RoundingOption.RoundUp :
                    retval = x - (x - intx) + 1;
                    break;
                case RoundingOption.RoundDown :
                    retval = x - (x - intx);
                    break;
                default:
                    var diff = x - intx;
                    if (diff >= .5M)
                        retval = x - (x - intx) + 1;
                    else
                        retval = x - (x - intx);
                    break;
            }
            return retval;
        }
    

    reply permalink

  • Ben - 10 years, 2 months ago

    Actually i like this better

        public enum RoundingOption
        {
            RoundDown,
            RoundUp,
            Normal
        }
    
        public Decimal Round(Decimal x, RoundingOption option)
        {
            int intx= (int)x;
            Decimal retval;
    
            if (option == RoundingOption.RoundUp || (option == RoundingOption.Normal && x - intx >= .5m))
                retval = x - (x - intx) + 1;
            else
                retval = x - (x - intx);
    
            return retval;
        }
    

    reply permalink

Content curated by @MaxBurstein