Link to home
Start Free TrialLog in
Avatar of salimdeath
salimdeath

asked on

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

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.
Salim
Avatar of hongjun
hongjun
Flag of Singapore image

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


hongjun
Avatar of salimdeath
salimdeath

ASKER

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.
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")
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Cheers.
Salim

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.