How to convert generic to double when generic could be a variety of types

I have looked at the other topics yet I cannot find the solution.

I am simply trying to add two values from an excel file.

The values are of the type “1000” or “1000.5” if there are decimals. The problem is when using “Get row item” it stores the output as STRING type generic variable (eg. “debits”) in the case of “1000” and DOUBLE type generic variable (eg. credits) in the case of “1000.5”, when I try to add/subtract these two variables eg. balance = debits - credits, I get an error : you cannot subtract a system double from a system string

When I try to convert to double first using double.parse(debits.tostring) I once again receive an error:
input string was not in correct format (refer attached)

See workflow and excel file attached
Main.xaml (15.8 KB) project.json (759 Bytes)
Transactions.xlsx (340.8 KB)

Use CDBL(generic value)

1 Like

Hi,

try this:System.Convert.ToDouble(strAmount.Trim)

I would hcuk a trim in there in case you’re picking up any whitespace

@HareeshMR Does not work

Didn’t realise you were using a generic type value, try this:

System.Convert.ToDouble(Generic.ToString.Trim)

Its working for me

image

1 Like

Maybe the generic variable isn’t quite in the right format?

@Ellboy @HareeshMR please refer to the workflow I have tried both these methods but they throw out the same exception. It needs to convert the “Get row item” from its generic value which is either “1000” if no decimal or “1000.5” if there is decimal ie. if there is no decimal ie. in the case of “1000” it is as “1000” straight not as “1000.0” so what do you suggest I can store the “Get row item” variable as in order for me to add/subtract? OR if I store it as generic variables in their current format how do I properly convert to any type which can be added or subtracted be that decimal/double/etc.Main.xaml (15.4 KB) project.json (759 Bytes)

What I want to do is subtract to values after converting them to double, if you try assign another value eg. 2.5 to another variable eg. gen2 and then convert to double using your method and then try and subtract gen from gen2 it will not work, when using “Get row item”

You don’t need to use the get row item activity. You just use an assign within the for each row:

dblVar = System.Convert.ToDouble(row(0))

Row items are indexable so you can call data within it simply like that.

Main(4).xaml (14.6 KB)

I can’t test it without your excel file but that should work

Still it is working for me

image

And why don’t you substract a generic value from a generic value directly without converting it to dbl?

Refer to original post the excel file is there “transactions.xlsx”

Make sure you check that it’s a number before converting.
If(IsNumeric(row(0)), CDbl(row(0)), 0)
or If(IsNumeric(row(0)), CDbl(row(0)), 0) - If(IsNumeric(row(0)), CDbl(row(0)), 0)

As another safe-guard, you can trim the value by changing row(0) to row(0).ToString.Trim

Also, try to avoid using Generic types. In my opinion, if the value is supposed to be a number, then use a Double or Integer type, and if it is a String use that type. If it can be either one, I use Object sometimes. A DataRow is an Object, which is why row(0) needs to be converted to your desired type before using the value.

Regards.

I tried that it does not work exception says “cannot subtract a string from a system double” when trying to directly subtract a generic from a generic.

It seems one of the value type is string there. Please check @Taariq

None of these methods are working let me try and break it down:

  1. I have read values from an excel table, stored in to a DataTable variable.
  2. The possible values in the columns [Debits] and [Credits] in the DataTable are of the type “0” or “0.0” (if there are decimals) or simply “” (ie. blank) if there is no values in the cells.
  3. I then want to take the certain debits and subtract the certain credits which I am unable to do because these are different data formats but all are numbers.
    What is the suggestion?

Actually, the row values will be of type “Object” so your work will be far easy to convert them from object to double,

image

@HareeshMR there is an exception please see workflow attached and exception screenshotUntitled .
Main.xaml (13.0 KB) project.json (759 Bytes)

This is because, you are getting null values while reading row(“debits”) or credits.

so use if condition to substract if there are values inside, otherwise move to next value

1 Like

You can resolve conversion errors by checking if it’s a number during the calculation.

See above suggestion quoted again here:

It let’s you use a 0 for any value that’s not a number

Regards