Conversion problem in Column Expression

Good Day Knimers :green_heart:

I am using Column Expression node for the following case, which works very fine:
image

But the problem is that it changes initial value
As you can see, it adds precision 74,453 = > 74,4532900000001
image

Can you give an advice how to solve this issue?

Many thanks, Karlygash

Hi @Karlygash , it might have to do with the Type. It looks like you are converting to string, and you kind of donโ€™t have choice since you might have โ€œNAโ€.

Any reason why you are not using Rule Engine for that? Try to do this with Rule Engine instead. There are some unpredictable behaviour with Javascript (Column Expressions) when dealing with doubles. My guess is that the values in the Replacement columns are more decimal precision than just 74.453, but the columns shows only 74.453.

EDIT: I just did a test, and it confirms my last comment about the precisions.
Manual input for Table Creator:
image

Table Creator after green state:
image

Column Expressions that simply copies the Replacement column to new column0, which is string (same as @Karlygash scenario):
image

I also tested with Rule Engine, and it will be the same behaviour. Itโ€™s all about the fact that the real number is not just 74.453 and when converted to string, the real number is revealed.

You can always format the string after to keep only 74.453.

4 Likes

Thanks a lot for your explanation
Do you mean formatting number using string manipulation, right?

Hi @Karlygash , since you are using Column Expressions, you can just do this directly there:

if(column("Replacement") == null) {
    "NA"
} else {
    column("Replacement").toFixed(3)
}

The function .toFixed(3) will keep it at 3 decimal places.

3 Likes

Hi @Karlygash , just checking if that answered your question.

2 Likes

Hi @bruno29a,

sorry I had too many questions in Knime forum, forgot to approve your solution

Thanks a lot :green_heart:

1 Like

No problem @Karlygash , itโ€™s more to know if it worked or not :smiley:

Thanks for confirming :slight_smile:

1 Like

thanks a lot=)
Column expression is really handy node)

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