简体   繁体   中英

return decimal with two decimal places C#

I'm trying to round decimal values with two decimal places, eg 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?

Use following method:

 Math.Round(57.328385899814471243042671610,2);

second parameter is no of decimal places

System.Math.Ceiling(c * 100) / 100);

像这样的事情可能会奏效。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM