繁体   English   中英

ASP.NET/C#十进制为0.00

[英]ASP.NET/C# Decimal to 0.00

我有了这种方法,如何将小数设为.00而不是.0000?

    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>

1234m.ToString(“ 0.00”)

您尚未显示它们的显示位置。 我们所拥有的只是一个数据库单元和一个内存中的对象,它们都只是存储。 出于存储目的, .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>    

打印Product.Price时,请使用.ToString(“ N2”)。 您可能想要的是完整的货币表达式,即.ToString(“ C”)。 这是所有格式字符串的参考链接:

http://msdn.microsoft.com/zh-CN/library/dwhawy9k.aspx

好吧,显然您不能执行以下操作

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

因为它将返回一个字符串。 尽管您可能可以这样做:

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

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

您担心文化背景吗?

检查此msdn链接: http : //msdn.microsoft.com/zh-cn/library/dwhawy9k.aspx

希望能帮到您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM