I add one data row into excel and use sum "total" column, it return 4.54747350886464E-12,in fact, it should return 0

Description:

  1. I have one excel with some data
  2. I add one data row
  3. I use DT_Test.AsEnumerable().Sum(Function(row) row.Field(Of Double)(“Total”))
  4. I can not get right sum value ,it give me wrong data (4.54747350886464E-12), in fact, the sum result should be zero

could you anyone help me ,thanks.

BlankProcess_Test20240111.zip (46.0 KB)

I attachment my process for your reference, thanks.

Hi @Lori

YourDataTable.AsEnumerable().Sum(Function(x) If(Not String.IsNullOrEmpty(x("Column Name").ToString()) or Not String.IsNullOrWhiteSpace(x("Column Name").ToString()), Convert.ToDecimal(x("Column Name")), 0)).ToString()

Regards

@Lori

DT_Test.AsEnumerable().Sum(Function(row) Math.Round(row.Field(Of Double)(“Total”), 2))

Math.Round(row.Field(Of Double)("Total"), 2) rounds each value in the “Total” column to 2 decimal places before summing them up. Adjust the number of decimal places according to your precision requirements.

Hi @Lori

YourDataTable.AsEnumerable().Sum(Function(x) If(Not String.IsNullOrEmpty(x("Total").ToString()) or Not String.IsNullOrWhiteSpace(x("Total").ToString()), Convert.ToDecimal(x("Total")), 0)).ToString()

Replace the YourDataTable with the actual DataTable Variable.

Regards

Hi,

Can you try to use Decimal instead of Double as the following, because it’s Double type accuracy matter?

DT_Test.AsEnumerable().Sum(Function(row) Decimal.Parse(row("Total").ToString))

Sample
BlankProcess_Test20240111v2.zip (13.9 KB)

Regards,

Hi @Lori
Please check the below image and I have attached the workflow for your reference.


BlankProcess_Test20240111.zip (90.3 KB)

Regards

Your code has the smallest changes, I test , however, it still return wrong data (4.54747350886464E-12)

Your method works well, however, thanks you very much!

1 Like

Hi @Lori

Thank You.

If you find the solution please do mark my solution as solution to close the loop.

Happy Automation!!

Regards

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.