2015-10-15 84 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