开发者

Formatting is Specified but argument is not IFormattable

开发者 https://www.devze.com 2022-12-30 08:02 出处:网络
string listOfItemPrices = items.ToSemiColonList(item => string.Format(\"{0:C}\", item.Price.ToString()));
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");
0

精彩评论

暂无评论...
验证码 换一张
取 消