Number to String

Hi KNIME members,

I tried to convert 11,312,918.2 into a string but I get this 1.13129182E7 as the output which is incorrect. My requirement is add 2 decimal places in to this number 11,312,918.2 where answer should be 11,312,918.20.

I went through other similar topics but no one contained the answer I am looking for. Could somebody help me please?

Hi @psycho

I do not know whether there exists a node in KNIME that converts doubles to integers or longs whilst preserving the format you want. For me, “11,312,918.20” is just a possible (more human) string representation of “1.13129182E7” among many possible different ones and eventually, if no node deals with this, the following workflow may be a solution:

Hope it helps.

Best
Ael

1 Like

Hi @psycho,
did you get the output within KNIME AP or did you write it to an Excel an you see it there?

@psycho

I just created a new KNIME node which uses Python to format numbers to strings. It uses the core Python functionality, so there is no need to install any additional Python dependencies.

The node uses the Python Format Specification Mini-Language to provide maximum flexibility in formatting the output numbers.

The node is available here, and there is a demonstration workflow here.

Example formatting:
8.2f - formats the number with 8 significant digits and two decimal places.

+8.2f - as above, but adds the sign.

.0f - Floating point number with no decimal places.

,8.2f - The comma adds thousand separators.

.2% - Formats the number as percentage with two decimal places.

0>4d - Formats an integer with four digits, right aligned and pad left with zeros.

X - Format output as hexadecimal.

There are plenty more examples on the internet if you search for Python formatting digits.

DiaAzul
LinkedIn | Medium | GitHub

4 Likes

Hello @psycho,

you can use Round Double node to convert double to String with no exponent and defined number of decimal places.

Br,
Ivan

1 Like

Hi @ipazin

The -Round Double- node rounds at the right number of decimals and allows the direct conversion of the rounded double to a string in different formats as shown here below, but non of them add comma separators to the string.

image

image

Am I missing anything at the node configuration ? Thanks in advance for your feedback.

Best
Ael

Hello @aworker,

you are not missing anything. There’s no comma as separator configuration option in this node. I just presented option to get string without exponent easily. Regarding number formatting there have been a lot of topics regarding it and believe there are one node solutions using regex (and probably couple of components) to deal with it. Here I share one:

Br,
Ivan

2 Likes

Hi @ipazin

Thanks for the feedback. I like your solution based on Java as suggested in your post. It is equivalent to the one by @DiaAzul but instead of being based on Python, yours is based on Java.

It would be nice to have a dedicated KNIME node to format numerical data once it is converted into strings in the way the .format() method does in Java and Python :blush:

Enjoy your day ahead!

Best
Ael

1 Like

@aworker

Unless I am missing something that is what the node I posted does. Are you saying you want a KNIME native node that does the same thing?

Hi @DiaAzul

Yes, this is what I said, a native node as part of the basic KNIME nodes.
I understand that your component does the job and this is great.

Best
Ael

1 Like

aworker,

Thank you for this solution it worked. The data set that I had to work was small data set hence I tried your solution first and it worked. I will try the same solution with a large data set and will let you know the outcome. thanks again.

1 Like

Hi @psycho

Thanks for the feedback and for validating the solution. Please let us know eventually if the solution is fast enough. Based on your feedback, an improved solution might be brought forward.

Best wishes,
Ael

1 Like

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