Solved

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

Posted on 2007-07-29
Medium Priority
170 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
• 4
• 2

LVL 33

Expert Comment

ID: 19587944
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

ID: 19587971
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

ID: 19587988
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

hongjun earned 1000 total points
ID: 19587994
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

ID: 19588291
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

ID: 19588296
:)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
###### Suggested Courses
Course of the Month16 days, 3 hours left to enroll