Rounding UP

Posts 17 of 7 · Page 1 of 1
Rounding UP
I'm sure you will be able to tell what I am trying to do here from looking at the code written.
I have looked at functions such as Math.Ceiling or Math****und but do not understand why this isn't working. Would someone mind explaining?

I'm trying to round up the batches needed to the nearest whole number.

Code:
namespace ConsoleApp1
{
    class Program
    {
        const int ONE_BATCH = 20;
        static void Main(string[] args)
        {
            float amountOfButtons, batchesNeeded;
            string userInput;
            Console.WriteLine("How many puttons will you be using?");
            userInput = Console.ReadLine();
            amountOfButtons = Convert.ToSingle(userInput);
            batchesNeeded = amountOfButtons / ONE_BATCH;
            Math.Ceiling(batchesNeeded);
            Console.WriteLine("You need to order {0} batches of buttons", batchesNeeded);
            Console.ReadLine();
 
        }
    }
}
Thank you for any help
Math.Ceiling always rounds up (towards the ceiling)
Math.Floor always rounds down (towards to floor)
Math. Round always rounds to nearest integer
Quote Originally Posted by BoobMax2442 View Post
Math.Ceiling always rounds up (towards the ceiling)
Math.Floor always rounds down (towards to floor)
Math. Round always rounds to nearest integer
My code only rounds to the nearest .2 for example but i want it to round up to the nearest whole number. Is there a way for me to do this with the code written?
"round up to the nearest whole number"
"Math. Round always rounds to nearest integer"


Math. Round(batchesNeeded);
I need it to round up though if that makes sense rather than rounding down if the number is below x.5

As you can see I have used Math.ceiling in my code but it still displays a decimal number for example 2.2 rather than 3

- - - Updated - - -

Resolved:
Code:
namespace ConsoleApp1
{
    class Program
    {
        const int ONE_BATCH = 20;
        static void Main(string[] args)
        {
            float amountOfButtons, batchesNeeded;
            string userInput;
            Console.WriteLine("How many buttons will you be using?");
            userInput = Console.ReadLine();
            amountOfButtons = Convert.ToSingle(userInput);
            batchesNeeded = (float)Math.Ceiling(amountOfButtons / ONE_BATCH);
            Console.WriteLine("You need to order {0} batches of buttons", batchesNeeded);
            Console.ReadLine();

        }
    }
}
Thank you everyone for trying to help.
First of all, you're dividing an integer by a float and then sending it to Math.Ceiling which is expecting a double. Is this intentional? What you want is:

Math. Round(1.2) ==> 1
Math. Round(1.5) ==> 2
Math. Round(2.5) ==> 2
Math. Round(2.5, MidpointRounding.AwayFromZero) ==> 3

MSDN Documentation for MathRound Method.
Is there a reason you're using floats? I doubt that you can order 0.2 or 2e20 buttons.. you should just use integers for this whole thing..

Code:
int input = ReadUserInput();
int batches = (input + batchSize - 1) / batchSize;
Posts 17 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?