Sum comma values in datable

Hello,

I have datable with values, most of them are values with decimals. I`m converting comma to dot and then sum in one variable as below. The problem is that method is incorrect. For example value:

12,1235 will turn in to 12.1235 ( it will read as 12 thousands instead of twelve with decimal ,1235 ) so my total sum is incorrect. :frowning:

For each row in datable:
CurrentRow(“values”).ToString.Replace(",",".")
Sum=Sum+Cdbl(CurrentRow(“values”)

Thank you from above!

HI @Add_Bddd ,

Do you want 12,000 to be read as twelve or twelve thousand?
image

If it has to be intepreted as 12, then you can sum all the values either using a For Each Activity like so:
Assign to Sum

Convert.ToDouble(CurrentRow("values").ToString.Replace(",","."))+Sum

Or by using LINQ(which is super fast, and recommended if your DataTable is large)->

Dt.AsEnumerable().Sum(Function(s) Convert.ToDouble(s("values").ToString.Trim.Replace(",",".")))

Kind Regards,
Ashwin A.K

1 Like

@Add_Bddd
We can sum the column values of all rows and its column by a single line statement

Assign Activity:
LHS: mySum | Datatype: Double
RHS:

dtData.AsEnumerable.Sum(Function (x) Double.Parse(x("values").toString.Trim, new System.Globalization.CultureInfo("de-DE")))

we would recommend to implement it via this Culture approach instead of String manipulations as we do have a controlled fail, when the format is not as expected. Also it avoids side effects as a valid string of 1.234,56 would not be changed to 1.234.56


Last line introduce the rounding, for more nice values

Find starter help here:
ColComputations_Sum_CommaSeperator.xaml (6.6 KB)

2 Likes

@ppr @ashwin.ashok

Both marked as solution, thank you so much guys!

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