ASP.NET/C# DECIMAL ~ 0.00
문제
이 방법을 얻었습니다. 10 진수를 .0000이 아닌 .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>
해결책
당신은 당신이 이것들을 어디에 표시하는지 보여주지 않았습니다. 우리가 가진 모든 것은 데이터베이스 셀과 메모리의 객체이며, 둘 다 단지 스토리지 일뿐입니다. 스토리지 목적으로 .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();
}
}
}
}
업데이트:
당신은 이것이 그리드 뷰에있을 것이라고 언급했습니다. 괜찮아. 이 경우, 당신이해야 할 일은 DataFormatString
다음과 같은 열 정의에서 :
<asp:GridView runat="server" id="TestView" ... >
<Columns>
<asp:BoundField DataFormatString="{0:F2}" />
...
</Columns>
</asp:GridView>
다른 팁
1234m.tostring ( "0.00")
Product.Price를 인쇄 할 때 .ToString ( "N2")을 사용하십시오. 당신이 원하는 것은 .TOSTRING ( "C") 인 전체 통화 표현식입니다. 다음은 모든 형식 문자열에 대한 참조 링크입니다.
분명히 당신은 다음을 할 수 없었습니다
product.Price = Convert.ToDecimal(reader["Price"]).ToString("0.00");
문자열을 반환합니다. 이 작업을 수행 할 수는 있지만 :
product.Price = Convert.ToDecimal(reader["Price"].ToString("0.00"));
이것은이 작업을 완료하는 가장 논리적 인 방법 인 것 같습니다.
문화 환경에 대해 걱정하십니까?
이 MSDN 링크를 확인하십시오.http://msdn.microsoft.com/en-us/library/dwawy9k.aspx
희망적으로 당신에게 손을 줘야합니다.