2015-10-15 103 views
1

我嘗試格式化編曲1在我的代碼currecny,但它不是我的ammount的專欄設置列格式爲貨幣

這裏踢得好看(翻譯=不工作)是我的代碼塊;

 public void TransactionLog() 
     { 

     listView1.View = View.Details; 
     listView1.GridLines = true; 
     listView1.FullRowSelect = true; 

     listView1.Columns.Add("Buy/Sell", 97); 
     listView1.Columns.Add("Amount", 95); 
     listView1.Columns.Add("Transaction ID", 100); 

     string[] arr = new string[3]; 
     ListViewItem item; 

     string URT = "https://api.eveonline.com/char/WalletTransactions.xml.aspx?keyID=4602486&&vCODE=BHGVeXQkRLKLkIkZQHdeyUxmUz9EfUwbvGzoc2eO4ZR8kRMYxk8PbD4LMwLF7BvH"; 
     XmlDocument XMLtrans = new XmlDocument(); 
     XMLtrans.Load(URT); 
     XmlNodeList TRnodelist = XMLtrans.SelectNodes("/eveapi/result/rowset/row"); 
     foreach (XmlNode xmlnode in TRnodelist) 
     { 
      if (xmlnode.Attributes["transactionType"] != null) 
       arr[0] = xmlnode.Attributes["transactionType"].InnerText; 
      if (xmlnode.Attributes["price"] != null) 
       arr[1] = xmlnode.Attributes["price"].InnerText; 
      if (xmlnode.Attributes["transactionID"] != null) 
       arr[2] = xmlnode.Attributes["transactionID"].InnerText; 
      item = new ListViewItem(arr); 
      listView1.Items.Add(item);  
     } 

我試過了;

string.Format(CultureInfo.CreateSpecificCulture("ja-JP"), "{C:0}", arr[1] = xmlnode.Attributes["price"].InnerText); 

但我只是得到錯誤。

enter image description here

+1

你是什麼意思_not play nice_ exactly exactly?你得到的結果是什麼,你想要得到什麼?你能舉個例子嗎? –

+0

嗯,我想格式化我的價格列作爲貨幣。 – Losec

+1

用'{0:C}'替換'{C:0}'。這就是我看到的代碼中的錯誤,看起來像一個簡單的錯字。 – bokibeg

回答

0

您必須更換{C:0} with {0:C}

string.Format(CultureInfo.CreateSpecificCulture("ja-JP"), "{0:C}", arr[1] = xmlnode.Attributes["price"].InnerText); 
+0

這hasen't工作,得到沒有格式爲我的列 – Losec

1

由於bokibeg commentedString.Format使用composite formatting功能。這裏是語法;

{index[,alignment][:formatString]} 

正如你可以看到,指數成分來字符串組件之前。

其他的事情是,The "C" format specifier是爲數字值。這意味着,你不能格式化一個字符串。

如果您xmlnode.Attributes["price"].InnerText返回一些有效的數值,你可以嘗試格式化之前適當Parse方法解析它。例如,如果這返回有效的int,則在格式化之前需要使用int.Parse()

string.Format(CultureInfo.CreateSpecificCulture("ja-JP"), 
       "{C:0}", 
       int.Parse(xmlnode.Attributes["price"].InnerText)); 
+0

得到na未處理的異常。 – Losec

+0

@Losec什麼'xmlnode.Attributes [「price」]。InnerText'完全返回? –

+0

即時通訊使用這個鏈接https://api.eveonline.com/char/WalletTransactions.xml.aspx?keyID=4602486&&vCODE=BHGVeXQkRLKLkIkZQHdeyUxmUz9EfUwbvGzoc2eO4ZR8kRMYxk8PbD4LMwLF7BvH和XML領域的「價格」 – Losec