cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Converting Text String list into Numbers (Float) _ [solved]

ivan3
Deputy Chef I
Deputy Chef I

Hello everyone! I'm sort of new to Workato and need some help!


I have to collect a list of values called Final Balance Due and then add them up to generate one final number. The problem is that the original datapill is a text string (even thoug it has only a number).

I used a Lists by Workato action to generate the list and I was able to clear its extra text by using the .pluck formula and (in the step 9) I ended up with a array/list that looks like this:


[nil, 4750.0, 11570.19, 3000.0]


When I try to use the .to_f formula I ge this error: Error calculating input for field 'value': undefined method `to_f' for #<Array:0x00007ff2cee20180>


but I can't convert this to float to be able to sum it... Any thoughts? Thank you in advance!! 


Here is my recipe: https://app.workato.com/recipes/2507257?st=7c5c792d31d65370e446c338841f64b1e070962785b860598c31a32d9...

10 REPLIES 10

gary1
Executive Chef III
Executive Chef III

Hi Ivan,


You can't use .to_f on an array because that is not a method in the array class. You can only use methods on their appropriate classes, so .to_f can only be used on integers (which are objects within your array, but not the array itself).


There are a couple of ways to sum the array. The fastest way with Ruby is to do:


[your array].compact.sum


Or, in your case:


[Items].pluck('final_balance')].compact.sum


The .compact method removes nil values from the array, which is important because nil values would cause .sum to error out. The .sum method simply sums all of the numbers left in the array -- floats and ints.


If your array is truly coming out as "[nil, 4750.0, 11570.19, 3000.0]", then this means your ints and floats are not actually strings. If they were strings, it would look like [nil, "4750.0", "11570.19", "3000.0"]. This would require some additional conversion before summing, and you'd likely have to pass the array through a loop.


If that is the case, the long way might be more applicable. You would need to accumulate the value in your loop, which could be done like this:


Create a number variable called "Total" before your loop (Step 5) and set the starting value to zero. Then, within your loop, add another "Update Variable" action to add "Total" + "Final Balance Due". This will accumulate as it iterates through each item in the loop.


You might also have to add an if conditional to prevent adding nil values, as that would result in another error. If the values were strings, this is where would you use .to_f to convert each index of the array to the floats before adding.


Hope this helps!

ivan3
Deputy Chef I
Deputy Chef I

It worked!!!! thank you much for the detailed response. I learned a lot.

christieparker
Deputy Chef I
Deputy Chef I

hey Christie!

andrew-leung
Deputy Chef I
Deputy Chef I

Hi Ivan,

To convert array elements from string to float and sum , you will need something like this:


test_array=["4.2", "6.7", "2.3"]


test_array.map(&:to_f).sum


Hope this helps