簡體   English   中英

ASP.net ListBox貨幣格式

[英]ASP.net ListBox currency formatting

好的,我有一個列表框,用於顯示綁定列表中的產品(它們是自定義類,並且具有ID,名稱,價格)。我希望列表框顯示商品名稱和價格。 列表框(lbProductsChosen)將“ DataTextField”設置為Name,將DataValueField設置為ID。 我正在使用PreRender事件檢查每個Item,從綁定列表(blProducts)等中查看其價格,並且效果很好。 我得到與此顯示在列表中的名稱和價格。 但是,顯示時,盡管我使用String.Format對其進行了格式化,但結果仍然是十進制數(例如3.20000),看起來很難看。 有誰知道為什么它要顯示它,但不顯示它如何格式化。

    protected void lbProductsChosen_PreRender(object sender, EventArgs e)
    {
        foreach (ListItem item in lbProductsChosen.Items)
        {
            string currentDescription = item.Text;
            int convertedValue = Convert.ToInt32(item.Value);
            for (int i = 0; i < blProducts.Count; i++)
            {
                if (blProducts[i].ProductID == convertedValue)
                {
                    decimal ItemPrice = blProducts[i].Price;
                    string convertedPrice = ItemPrice.ToString();
                    string currentPrice = String.Format("{0:c}", convertedPrice);
                    string currentDescriptionPadded = currentDescription.PadRight(30);
                    item.Text = currentDescriptionPadded + currentPrice;
                }
            }
        }
    }

MSDN聲明有關String.Format方法的以下內容。

通常,參數列表中的對象通過使用Culture的約定(由CultureInfo.CurrentCulture屬性返回)轉換為其字符串表示形式。

如果您使用貨幣格式說明符c ,它將使用系統設置中定義的貨幣格式。 control panel -> region -> additional settings -> currency查看。 也許您已經弄亂了那里的設置。

在此處輸入圖片說明


如果要忽略對貨幣有意義的本地設置,則可以執行以下操作。 (以美元為例,總是兩位小數)

decimal price = 12.10999999m;
String.Format("${0:0.00}", price);

$ 12.10條

請注意, String.Format不能正確舍入該數字,而只是將其截斷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM