Rounding to the next whole number (E.g. 1.0124 becomes 2)

Posted on 2007-07-29
Last Modified: 2013-11-27
Hello Team,

I have got a special rounding to do and after many search performed in EE, i could not come across a solution.  Can you please help me with the problem below;

After a calculation has been performed in my coding part, the result are displayed in a testbox.  I DO NOT want the decimals to appear, all i need is that the result is rounded to the next whole number.  The moment the value is over .0 something, it should be rounded to the next whole number.  Please see example below;

Result being displayed   ---->   What i want to get
       8.9 -------------------------------> 9
       8.1 -------------------------------> 9
       8.487 ----------------------------> 9
       8.091 ----------------------------> 9
       1.254 ----------------------------> 2
       1.014 ----------------------------> 2
       1.9745 ---------------------------> 2
       0.0021 ---------------------------> 1

Looking ahead to hear your solutions team,

Thanks a lot for your help,
Kind regards.
Question by:salimdeath
    LVL 33

    Expert Comment

    Try this.
    You can just customize it and then put it in your own code.

                double d;

                d = 8.0;
                Console.WriteLine(Math.Ceiling(d).ToString("0")); // 8
                d = 8.2;
                Console.WriteLine(Math.Ceiling(d).ToString("0")); // 9
                d = 8.9;
                Console.WriteLine(Math.Ceiling(d).ToString("0")); // 9


    Author Comment

    Hi Hongjun,

    Thanks for replying.  

    I tried what you proposed, error displayed is "Expression Does not produce a Value".  Here's my lines of codes that is displaying the decimals;

    DataGridView1.Rows(0).Cells("QtyReq").Value = (Convert.ToInt32(TxtQtyOrder.Text) * Convert.ToInt32(TxtConeMeterage.Text)) / Convert.ToInt32(DVDGridYarnUse(0)("Meterage"))

    Any other possible solutions ?

    Thanks again for helping.
    LVL 33

    Expert Comment

    try this

    DataGridView1.Rows(0).Cells("QtyReq").Value = Math.Ceiling((Convert.ToInt32(TxtQtyOrder.Text) * Convert.ToInt32(TxtConeMeterage.Text)) / Convert.ToInt32(DVDGridYarnUse(0)("Meterage"))).ToString("0")
    LVL 33

    Accepted Solution

    I suggest you use ToDouble to avoid rounding errors.

    DataGridView1.Rows(0).Cells("QtyReq").Value =
    Math.Ceiling((Convert.ToDouble(TxtQtyOrder.Text) * Convert.ToDouble(TxtConeMeterage.Text)) / Convert.ToDouble(DVDGridYarnUse(0)("Meterage"))).ToString("0")

    Author Comment

    Hi Hongjun,

    Wow....... this one works like a charm !!! THANKS SO MUCH.
    Beside of helping me with an appropriate solution, your response was very quick.  I appreciate a lot.

    Thanks again dude, you deserve the points.

    PS: I got another issue on Date Calculation and Time Calculation.  I hope you'll be around when i post it soon.  Am not posting it now, cause i want to make sure that i searched everywhere.

    LVL 33

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now