Adding Preceding Zeros to a Column

I tried to use the Java snippet node with the below code :

return String.format("%02d",$column1$);
but it is giving me an error “void method cannot return a Value”.

How to solve this ?

Hi @namankumar169

JavaSnippets and Regex are not my kind of expertise. But you can solve this with regular KNIME nodes.

. See this worklow add_0.knwf (17.4 KB)

gr. Hans

3 Likes

Hi,
you can use the padLeft function in the String Manipulation node. First you need to convert your number to a string, though. This is necessary anyways, as leading zeros are removed for number types.
Kind regards
Alexander

7 Likes

Hi @namankumar169,

I have posted this answer already in this topic where you asked the same question. I am going to close the other one.

However, another easy way to solve this task is using the Column Expressions node with this script:

if (length(column("column1"))<2) {
    "0" + column("column1")
} else {
    column("column1")
}

column1 must be replaced by your column name. Here is also an example workflow:add_leading_0.knwf (6.2 KB)

Cheers,
Simon

5 Likes

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