Solved

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

Posted on 2007-07-29
164 Views
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

Thanks a lot for your help,
Kind regards.
Salim
0
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

hongjun
0

Author Comment

Hi Hongjun,

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.
0

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")
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")
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.
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.

0

LVL 33

Expert Comment

:)
0

## Featured Post

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…