How-to | Pad a number with leading zeros#

A common requirement when you have a column of numbers is to format all numbers so that they have the same length, adding leading zeros if needed. This can be done in the Prepare recipe using a Formula.

Format function#

The function to use is format. For example, to ensure that all values of the column mycolumn are padded to have a length of 11, including leading zeros, use: format("%011d", mycolumn)

../../_images/format-pad-zeros.png

It helps to know the usual formula rules to refer the values of columns, as described in the Dataiku reference documentation.

For example, if working with a column named my column (note the space), you would instead use format("%011d", numval("my column"))

See also

Beyond this use case, format can be a very powerful function that uses Java string formatting capabilities. See this Java reference for more details.