2015-09-19 38 views
1

我已經看遍了所有的地方!我發誓!即時通訊嘗試讓我的輸出顯示「$」和兩個小數點右邊的期間。即時通訊使用C#顯示美元符號和兩位小數

//declare variable 
       decimal decInputDays; 
       decimal decInputAmountofBooks; 
       decimal decOutputAmountofFine; 

       decimal FINE_CALCULATE = .05m; 

       //get values from the form 
       decInputDays = decimal.Parse(txtDays.Text); 
       decInputAmountofBooks = decimal.Parse(txtBooks.Text); 


       //determine fine amount 
       decOutputAmountofFine = decInputDays * decInputAmountofBooks * FINE_CALCULATE; 
       //display fine amount 

       lblAmount.Text = decOutputAmountofFine.ToString("c"); 
+0

對不起,使用im C# – ntrnlerror9

回答

0

你需要

decimalVal.ToString("C2") 
+0

試過了....仍然沒有我的最後一行是lblAmount.Text = decOutputAmountofFine.ToString(「C2」);並沒有改變任何顯示 – ntrnlerror9

0

確切的輸出格式,將取決於它正在運行的計算機上當前的文化選擇,但ToString("C")應該產生與貨幣符號輸出,千位分隔符哪裏需要加上兩位小數。如果您需要更多或更少的小數位,您可以指定一個數字,但默認的2位數是最常見的。

如果一切都失敗了,你應該能夠迫使這樣的格式:

lblAmount.Text = string.Format("${0:#,0.00}", decOutputAmountofFine); 

或者,如果您正在使用C#版本6(VS2015):

lblAmount.Text = $"${decOutputAmountofFine:#,0.00}"; 

注意#,是「插入千位分隔符」說明符,仍然可能受本地化影響......但我不知道任何使用除3以外的值作爲分隔符距離的語言環境(System.Globalization.NumberFormatInfo.CurrencyGroupSizes)。檢查System.Globalization.CultureInfo.CurrentCulture.NumberFormat中的值以查看爲您的位置配置了什麼。

0

您沒有指定您當前的結果。但不管它是什麼,這聽起來像你的問題是文化相關的。

考慮強迫文化en-US。它應該給你你正在尋找的貨幣格式:

decOutputAmountofFine.ToString("c", CultureInfo.GetCultureInfo("en-US"));