我有以下代码-
Label1..Text = dt.AsEnumerable().Sum(x => x.Field<decimal?>("col1") ?? 0).ToString("#,#.####", CultureInfo.InvariantCulture);
在这里,我将“col1”的总和显示到标签上。
我正在检查 x.Field<decimal?>("col1")
对于空值
问题是,如果列的值类似于 1234,它会正确显示,但如果 x.Field<decimal?>("col1")
为 null,则它将值为零,标签上不显示任何内容。
似乎 toString 会忽略该值为零且不返回任何值。
请帮忙
最佳答案
使用格式 "#,0.####"
这将显示 0
表示 0 个值,1,234
表示 1234
Label1.Text = dt.AsEnumerable()
.Sum(x => x.Field<decimal?>("col1") ?? 0)
.ToString("#,0.####", CultureInfo.InvariantCulture);
您应该看到:Custom Numeric Format Strings.
"0" - Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
关于c# - 如果值为零,则 Decimal.ToString 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22381592/