string listOfItemPrices = items.ToSemiColonList(item => string.Format("{0:C}", item.Price.ToString()));
I am simply trying to format the pri开发者_运维技巧ce here to 2 decimal places. Ok, so the string.Format doesn't implement IFormattable? Ok not sure how to get around this so that I can format the decimal (price) here.
By passing item.Price.ToString()
to String.Format
, you are passing a string, not a decimal.
Since strings cannot be used with format strings, you're getting an error.
You need to pass the Decimal
value to String.Format
by removing .ToString()
.
There is no point using string.format here, that is used for adding formatted values into strings. e.g.
String.Format("This is my first formatted string {O:C} and this is my second {0:C}",ADecimal,AnotherDecimal)
If you just want the value of a decimal variable as a formatted string then just pass the string formatter to the ToString() method e.g.
ADecimal.ToString("C");
精彩评论