2009-10-23 77 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")); 

这似乎是完成此操作的最合乎逻辑的方式。