开发者

ASP.NET/C# Decimal to 0.00

I got this method, how can I make the decimal to .00 and not .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;
}

UPDATE: Sorry for asking stupid questions. I can't see where to put the DataFormatString="{0:F2}"

This is my grid:

            <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" Te开发者_高级运维xt='<%# Bind("Price") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


1234m.ToString("0.00")


You haven't shown where you are displaying these. All we have is a database cell and an object in memory, both of which are merely storage. For storage purposes, .00 and .0000 are the same thing. Trying to translate from one to the other is a waste of resources. Show us where you display this to the user, and we'll help you format it however you want.

Also, as a personal preference I'd write that code like this:

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

Update:
You commented that this will be in a GridView. Okay. In that case, all you need to do is have a DataFormatString in the column definition, like this:

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


When printing Product.Price, use .ToString("N2"). What you might want is the full currency expression, which is .ToString("C"). Here is a reference link for all the format strings:

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx


Well obviously you couldn't do the following

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

as it would return a string. Though you may beable to do this:

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

This would seem the most logical way to complete this operation.


Are you worried about culture settings?

Check out this msdn link: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Should hopefully give you a hand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜