return decimal with two decimal places C#

491 views Asked by At

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?

3

There are 3 answers

4
M.S. On

Use following method:

 Math.Round(57.328385899814471243042671610,2);

second parameter is no of decimal places

0
Nicholas On
System.Math.Ceiling(c * 100) / 100);

Something like this would probably work.

1
Grant Winney On

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.