-3

I have to display the values in DataTable with Exactly Two Decimal places. I want to display like this to looks like a time format. (I apologize for my English) thank you.

       protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("SOT", typeof(Decimal));
        dt.Columns.Add("SVT", typeof(Decimal));
        dt.Columns.Add("SCT", typeof(Decimal));
        dt.Rows.Add(12.22,23.44,22.00);
        dt.Rows.Add(2.20, 23.00, 22.23);
        dt.Rows.Add(12.00, 23.20, 22.05);
            GridView1.DataSource = dt;
            GridView1.DataBind();
    }
    catch (Exception d)
    {
        Response.Write(d.Message.ToString());
    }
}

DataTable Can take values like (2.2,23,22.5) not (2.20,23.00,22.5). Is there any method to insert values in DataTable like (2.20,23.00,22.5)

Mathis
  • 41
  • 1
  • 9

3 Answers3

3

You can use the String.Format methods. You can use the following:

string.Format("{0:f2}", YourDecimalValue);
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
0

This, perhaps?:

Value.ToString(".00")
Sean
  • 442
  • 3
  • 6
  • 19
0

I think this is what you need:

decimal a = 5;
decimal b = 6;

Console.WriteLine(string.Format("{0:#,###.00}, {1:#,###.00}", a , b));
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48