我有一个 GridView,它以编程方式绑定到 aList<T>
其中 T 是我的自定义对象,例如OrderItem
. 小数类型值显示许多小数位。我想将它们四舍五入到小数点后 2 位。我怎么做?
这大概是我所拥有的:
public class OrderItem
{
public Product Product { get; set; }
public int Quantity { get; set; }
public decimal GrossAmount { get; set; }
}
public class Product
{
public string ProductName { get; set; }
public decimal Price { get; set; }
public decimal DiscountPercent { get; set; }
}
<asp:GridView ID="dgOrderItems" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:TemplateField HeaderText = "Product">
<ItemTemplate>
<%# Eval("Product.ProductName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Price">
<ItemTemplate>
<%# Eval("Product.Price") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField = "Quantity" HeaderText = "Quantity" />
<asp:BoundField DataField = "GrossAmount" HeaderText = "Gross Amount" />
<asp:TemplateField HeaderText = "Discount (%)">
<ItemTemplate>
<%# Eval("Product.DiscountPercent") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>