c# - 如果值为零,则 Decimal.ToString 不起作用

标签 c# linq sum

我有以下代码-

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/

相关文章:

python - 如何获取 n*m 矩阵行的所有可能和的列表

php - SUM重复的mysql php

C# 3.0 : Need to return duplicates from a List<>

c# - Linq 链接 - 即使是正确的谷歌问题也可以

c# - 将 List<XElement> 转换为 DataTable

c# - 从字符串中解析一个值

python - 计算并求和所有先前的同类项

c# - 如果文件夹中没有某些 dll,则无法运行工具包?/

c# - C# 中的浅复制现实生活用法?

c# - 具有任意列和行的 Wpf DataGrid 数据绑定(bind)