2009-10-23 78 views
0

我得到了這個方法,我怎樣才能讓小數點爲.00而不是.0000?ASP.NET/C#十進制到0.00

public static List<Product> GetAllProducts() 
{ 
    List<Product> products = new List<Product>(); 
    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()) 
       { 
        Product product = new Product(); 
        product.Id = Convert.ToInt32(reader["Id"]); 
        product.ManufacturerId = Convert.ToInt32(reader["ManufacturerId"]); 
        product.CategoryId = Convert.ToInt32(reader["CategoryId"]); 
        product.Name = (reader["Name"]).ToString(); 
        product.Description = (reader["Description"]).ToString(); 
        product.Price = Convert.ToDecimal(reader["Price"]); 
        product.ItemsInStock = Convert.ToInt32(reader["ItemsInStock"]); 

        products.Add(product); 
       } 
      } 
     } 
    } 
    return products; 
} 

更新: 對不起,要求愚蠢的問題。 我看不到的地方把DataFormatString = 「{0:F2}」

這是我的網格:

  <asp:TemplateField HeaderText="Price" SortExpression="Price"> 
       <EditItemTemplate> 
        <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="PriceLabel" runat="server" Text='<%# Bind("Price") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 

回答

4

你的天堂」 t顯示你正在顯示這些的位置。我們所擁有的只是一個數據庫單元和一個內存中的對象,這兩者都只是存儲。出於存儲目的,.00.0000是同樣的事情。試圖從一個轉換到另一個是浪費資源。向我們展示您向用戶展示的位置,我們會幫助您根據需要進行格式化。

而且,由於個人喜好我會寫這樣的代碼:

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<Product> 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(); 
      } 
     } 
    } 
} 

更新:
你的評論,這將是在GridView。好的。在這種情況下,你需要做的是在列定義一個DataFormatString,像這樣:

<asp:GridView runat="server" id="TestView" ... > 
    <Columns> 
     <asp:BoundField DataFormatString="{0:F2}" /> 
     ... 
    </Columns> 
</asp:GridView>  
+0

順便說一句,這說明了我喜歡的Visual Basic更好幾件事情之一。想象一下,這是名字空間最後一堂課的最後一種方法。該文件的結尾將有6個嵌套的大括號(})。 VB結束了更容易閱讀。 – 2009-10-23 18:22:00

+0

對不起,我打印到GridView。 我不能使用product.Price = Convert.ToDecimal(reader [「Price」]。ToString(「0.00」));因爲它期望一個整數 – user83713 2009-10-23 18:25:51

+0

但我會嘗試你的解決方案 – user83713 2009-10-23 18:26:23

5

1234m.ToString( 「0.00」)

0

很明顯嘛,你不能做以下

product.Price = Convert.ToDecimal(reader["Price"]).ToString("0.00"); 

,因爲它會返回一個字符串。雖然你可能會這樣做:

product.Price = Convert.ToDecimal(reader["Price"].ToString("0.00")); 

這似乎是完成此操作的最合乎邏輯的方式。