I have following code-
Label1..Text = dt.AsEnumerable().Sum(x => x.Field<decimal?>("col1") ?? 0).ToString("#,#.####", CultureInfo.InvariantCulture);
here, I am displaying the sum of 'col1' onto a label.
I am checking x.Field<decimal?>("col1")
for null values
the problem is, if the value of a column is something like 1234, it displays properly, but if x.Field<decimal?>("col1")
is null then it takes the value as ZERO and nothing displays on the label.
It seems like toString ignores the value if it is zero and returns nothing.
Please help
Use format "#,0.####"
That will show 0
for 0 values and 1,234
for 1234
Label1.Text = dt.AsEnumerable()
.Sum(x => x.Field<decimal?>("col1") ?? 0)
.ToString("#,0.####", CultureInfo.InvariantCulture);
You should see: Custom Numeric Format Strings.
"0" - Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With