column expressions node better way to use string numbers

Hi,
i found out that in the column expressions node one can interestingly multiply numbers that are saved as string and add them with no problem. Like:

stringNumber1 =string(5)
stringNumber2= string(10)

Method1
1 x stringNumber1+ 1 x stringNumber2=15
it gives the correct result.

But if i write:
Method2:
stringNumber1 + stringNumber2=510
So weirdly it attaches them together like using a joiner.

That Method 1 works is probably because the column expressions node is based on JavaScript and i found an example in the internet that one can do this in JavaScript. But it somehow needs a multiplicatoin if you use “+” it attaches them together like 2 strings.

But i still wonder if this is safe, has anyone experience with this? or is there a better way of using string numbers in the column expressions node?

3 Likes

Hi,

That behavior is expected for javascript.

When you add strings together they simply concatenate.

But in your first example, you cast the strings to a numeric type because you multiplied by one.

I don’t recommend using string numbers; you should convert that column of strings to numbers first (using string numbers is confusing and not best practice).

3 Likes

Hi Victor,
yeah i read later that day more about this. Like in your second llink it kinda always sound that this is just a normal thing to do. That this just starts the type-change of the the variable and sind JS is dynamically typed anyways. I don’t understand what would be the difference if i make it with toint an integer first.
Or are there other things other than “readability”/more confusing in the CE node to consider?

How would you transform it? With toint in the CE node or does it have to be on another previous node changing it?

Have a good Whitsunday…

1 Like

In the programming languages I know, there are many ways to do the same thing. It’s just preference. For readability, I would use a function and not multiply by 1 to make it clear exactly what you are trying to achieve.

I would first transform the entire column from string to int using a node or function and then do the addition. (So in the previous node). This makes the Column Expression node much cleaner and easier to read which is best practice in code-writing.

2 Likes

Hi Victor,
thanks for your input on this…An extra node is a bit cumbersome but i will think about it…

Have a good one…

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