Skip to content

Fills the string with zeroes.

Source code

Description

Add zeroes to a string until it reaches n characters. If the number of characters is already greater than n, the string is not modified.

Usage

<Expr>$str$zfill(alignment)

Arguments

alignment Fill the value up to this length. This can be an Expr or something coercible to an Expr. Strings are parsed as column names.

Details

Return a copy of the string left filled with ASCII ‘0’ digits to make a string of length width.

A leading sign prefix (‘+’/‘-’) is handled by inserting the padding after the sign character rather than before. The original string is returned if width is less than or equal to len(s).

Value

Expr

Examples

library("polars")

some_floats_expr = pl$lit(c(0, 10, -5, 5))

# cast to String and ljust alignment = 5, and view as R char vector
some_floats_expr$cast(pl$String)$str$zfill(5)$to_r()
#> [1] "000.0" "010.0" "-05.0" "005.0"
# cast to int and the to utf8 and then ljust alignment = 5, and view as R
# char vector
some_floats_expr$cast(pl$Int64)$cast(pl$String)$str$zfill(5)$to_r()
#> [1] "00000" "00010" "-0005" "00005"