2010-05-17 37 views
6
string listOfItemPrices = items.ToSemiColonList(item => string.Format("{0:C}", item.Price.ToString())); 

我只是试图将价格格式设置为2位小数。好的,所以string.Format没有实现IFormattable?好吧不知道如何解决这个问题,以便我可以在这里格式化小数点(价格)。指定了格式但参数不是IFormattable

回答

14

通过将item.Price.ToString()传递给String.Format,您传递的是字符串,而不是小数。
由于字符串不能与格式字符串一起使用,因此出现错误。

您需要将Decimal的值通过删除.ToString()的值传递给String.Format

+0

我得到同样的搭配: inFile.setINVQueryDept(的String.Format( “{0:#0.00}”,frmCentral.startDept)的String.Format( “{0:#0.00}”, frmCentral.endDept)); – 2014-03-19 16:47:42

1

在这里使用string.format毫无意义,它用于将格式化值添加到字符串中。例如

String.Format("This is my first formatted string {O:C} and this is my second {0:C}",ADecimal,AnotherDecimal) 

如果只想小数变量作为格式化的字符串的值然后只是将字符串传递格式化到ToString()方法例如

ADecimal.ToString("C"); 
相关问题