简体   繁体   中英

Formatting is Specified but argument is not IFormattable

string listOfItemPrices = items.ToSemiColonList(item => string.Format("{0:C}", item.Price.ToString()));

I am simply trying to format the price 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. eg

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 eg

ADecimal.ToString("C");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM