Decimal.ToString does not work if the value is zero

I have the following code -

Label1..Text = dt.AsEnumerable().Sum(x => x.Field<decimal?>("col1") ?? 0).ToString("#,#.####", CultureInfo.InvariantCulture);

here, I show the sum of 'col1' on the label.
I check x.Field<decimal?>("col1")for null values

the problem is that if the column value is 1234, it displays correctly, but if x.Field<decimal?>("col1")it is null, it takes the value ZERO and nothing is displayed on the label.

It seems that toString ignores the value if it is zero and returns nothing.

Please, help

+4
source share
1 answer

Use format "#,0.####"This will show 0for 0 values ​​and 1,234for1234

Label1.Text = dt.AsEnumerable()
                 .Sum(x => x.Field<decimal?>("col1") ?? 0)
                 .ToString("#,0.####", CultureInfo.InvariantCulture);

: .

"0" - , ; .

+9

Source: https://habr.com/ru/post/1531591/


All Articles