Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How could I add Math.Round or Ceiling to my code to round up or Down?
I am importing an Excel file to a Datagridview and doing the math.




C#
private void button2_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow rows in dataGridView1.Rows)
            {
                


                int H = Convert.ToInt32(rows.Cells[7].Value ?? 0);
                decimal I = Convert.ToDecimal(rows.Cells[8].Value ?? 0);
                int L = Convert.ToInt32(rows.Cells[11].Value ?? 0);
                var J = H == 0 ? 0: H * I / L;
                rows.Cells[9].Value = J;

                //ULD Stack =rounddown(106/S2,0)
                decimal S = Convert.ToDecimal(rows.Cells[18].Value ?? 0);
                int A = 106;
                decimal T = A / S;
                rows.Cells[19].Value = T;

                //Stacks Wide =rounddown(96/R2,0) 
                int R = Convert.ToInt32(rows.Cells[17].Value);
                int B = 96;
                int U = B / R;
                rows.Cells[20].Value = U;

                //Stacks Day =roundup(J2/T2,0)                              
                double j = Convert.ToDouble(rows.Cells[9].Value ?? 0);
                double t = Convert.ToDouble(rows.Cells[19].Value ?? 0);
                int V = (int)Math.Ceiling(Convert.ToDouble(j) / Convert.ToDouble(t));
                rows.Cells[21].Value = V;

                //Linear FT Stack =Q2/12
                Decimal Q = Convert.ToInt32(rows.Cells[16].Value ?? 0);
                var C = 12 ;
                Decimal W = Q / C;
                rows.Cells[22].Value = W;

                //Raw Linear Ft Day =(V2*W2)/U2                                    
                int V21 = Convert.ToInt32(rows.Cells[21].Value ?? 0);
                var W22 = Convert.ToDecimal(rows.Cells[22].Value ?? 0);
                int U20 = Convert.ToInt32(rows.Cells[20].Value ?? 0);
                var X = V21 == 0 ? 0 : V21 * W22 / U20;
                rows.Cells[23].Value = X;}


What I have tried:

Lots of research trial and mostly error.
Posted
Updated 4-Nov-18 10:22am
Comments
Afzaal Ahmad Zeeshan 4-Nov-18 15:26pm    
And what is the error that you encounter?
Sinisa Hajnal 5-Nov-18 6:54am    
.NET has Math library that has everything for your rounding needs.

1 solution

you can get ceiling like this
// Get ceiling of double value.
       double value1 = 123.456;
       double ceiling1 = Math.Ceiling(value1);


and you can read

Math.Round Method (System) | Microsoft Docs[^]

using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("{0,5} {1,20:R}  {2,12} {3,15}\n", 
                        "Value", "Full Precision", "ToEven",
                        "AwayFromZero");
      double value = 11.1;
      for (int ctr = 0; ctr <= 5; ctr++)    
         value = RoundValueAndAdd(value);

      Console.WriteLine();

      value = 11.5;
      RoundValueAndAdd(value);
   }
   
   private static double RoundValueAndAdd(double value)
   {
      Console.WriteLine("{0,5:N1} {0,20:R}  {1,12} {2,15}", 
                        value, Math.Round(value, MidpointRounding.ToEven),
                        Math.Round(value, MidpointRounding.AwayFromZero));
      return value + .1;
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900