Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You haven't shown where you are displaying these. All we have is a database cell and an object in memory, both of which are merely storage. For storage purposes, <code>.00</code> and <code>.0000</code> are <em>the same thing.</em> Trying to translate from one to the other is a waste of resources. Show us where you display this to the user, and we'll help you format it however you want.</p> <p>Also, as a personal preference I'd write that code like this:</p> <pre><code>private static ToProduct(this IDataRecord record) { var product = new Product(); product.Id = record.GetInt32(record.GetOrdinal("Id")); product.ManufacturerId = record.GetInt32(record.GetOrdinal("ManufacturerId")); product.CategoryId = record.GetInt32(record.GetOrdinal("CategoryId")); product.Name = record.GetString(record.GetOrdinal("Name")); product.Description = record.GetString(record.GetOrdinal("Description")); product.Price = record.GetDecimal(record.GetOrdinal("Price")); product.ItemsInStokc = record.GetInt32(record.GetOrdinal("ItemsInStock")); return product; } public static IEnumerable&lt;Product&gt; GetAllProducts() { string sqlQuery = "SELECT * FROM Products"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(sqlQuery, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection)) { while (reader.Read()) { yield return reader.ToProduct(); } } } } </code></pre> <p><strong>Update:</strong><br> You commented that this will be in a GridView. Okay. In that case, all you need to do is have a <code>DataFormatString</code> in the column definition, like this:</p> <pre><code>&lt;asp:GridView runat="server" id="TestView" ... &gt; &lt;Columns&gt; &lt;asp:BoundField DataFormatString="{0:F2}" /&gt; ... &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload