简体   繁体   English

格式化是指定的,但参数不是IFormattable

[英]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. 我只是想把这里的价格格式化为2位小数。 Ok, so the string.Format doesn't implement IFormattable? 好的,所以string.Format没有实现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. 通过将item.Price.ToString()传递给String.Format ,您传递的是字符串 ,而不是小数。
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() . 您需要通过删除.ToString()Decimal值传递给String.Format

There is no point using string.format here, that is used for adding formatted values into strings. 这里使用string.format是没有意义的,它用于将格式化的值添加到字符串中。 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 如果您只想将十进制变量的值作为格式化字符串,那么只需将字符串格式化程序传递给ToString()方法,例如

ADecimal.ToString("C");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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