-1

I'm trying to round decimal values with two decimal places, e.g. I have 57,328385899814471243042671610 and I want 57.33.

     for (int i = 1; i < cobtable.Columns.Count; i++) //loop para preencher a linha das percentagens
        {
            Decimal a = (Decimal)cobtable.Rows[2][i];
            Decimal b = (Decimal)cobtable.Rows[0][i];
            cobtable.Rows[3][i] = Decimal.Divide(a, b) * 100;

           // Decimal c = (Decimal)cobtable.Rows[3][i];

          // c = (a / b) * 100;

          //  c = Math.Round(c,2);

           Response.Write((Decimal)cobtable.Rows[3][i]);
           Response.Write("*");
        }

       GroupGrid.DataSource = cobtable;
       GroupGrid.DataBind();

the c variable gives me on response.write() the correct value but in the grid it doesn't show the same value, it shows 57,328385899814471243042671610. What am I doing wrong?

Spider man
  • 3,224
  • 4
  • 30
  • 42
Mara Pimentel
  • 317
  • 1
  • 8
  • 14

3 Answers3

0

Use following method:

 Math.Round(57.328385899814471243042671610,2);

second parameter is no of decimal places

M.S.
  • 4,283
  • 1
  • 19
  • 42
0
System.Math.Ceiling(c * 100) / 100);

Something like this would probably work.

Nicholas
  • 783
  • 2
  • 7
  • 25
0

Specify a DateFormatString for the column in question in your GridView.

DataFormatString="{0:F2}"  // Rounds to 2 decimal places

This affects how the value appears to the user, without causing actual data loss from rounding.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165