- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
string listOfItemPrices = items.ToSemiColonList(item => string.Format("{0:C}", item.Price.ToString()));
我只是想将此处的价格格式化为小数点后两位。好的,所以 string.Format 没有实现 IFormattable?好的,我不确定如何解决这个问题,以便我可以在此处设置小数点(价格)的格式。
最佳答案
通过将 item.Price.ToString()
传递给 String.Format
,您传递的是一个字符串,而不是小数。
由于字符串不能与格式字符串一起使用,因此会出现错误。
您需要通过删除 .ToString()
将 Decimal
值传递给 String.Format
。
关于c# - 指定格式但参数不是 IFormattable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2849688/
IFormattable有没有好的引用实现?我打算至少定制一个 IFormatProvider对于我的对象,我想确保传递给 IFormattable.ToString(string, IFormatP
string listOfItemPrices = items.ToSemiColonList(item => string.Format("{0:C}", item.Price.ToString()
String.Format 和 IFormattable.ToString(format, value) 在尝试格式化为十六进制时提供不同的结果。使用 IFormattable.ToString(fo
我有一个处理字符串输入的函数。 public string Foo(string text) { // do stuff with text and /
我在将数字转换为 IFormattable、在其上调用 ToString(...) 并传递 from 0.000;-;0 的 FormatCode 时遇到问题,这意味着我想显示正数精确到小数点后三位,
我对 .NET 中不同形式的序列化缺乏一致性感到有点沮丧: DataContractSerializer - 使用新属性或旧的 [Serializable] 属性,但序列化程序本身并不像其他一些 WC
FormattableString 已在 C# 6.0 中引入。因为我们可以使用 string 对象使用相同的字符串格式,所以为什么需要使用 FormattableString 或 IFormatta
ReSharpers StringFormatMethodAttribute 是跟踪执行字符串格式化的方法的参数的好方法。 所以我以这种方式实现了一个方法: [StringFormatMethod("
IFormattable、IFormatProvider和ICustomFormatter有什么区别和联系,什么时候用?一个简单的实现示例也会非常好。 我并不是说什么时候在 .net 框架中使用它,而
第一次在 .net C# 中使用二进制格式 来自MSDN的代码是这样的: IFormatter formatter = new BinaryFormatter(); Stream stream =
我是一名优秀的程序员,十分优秀!