Skip to contents

This function calculates the number of decimal places in a numeric value. It is designed to work with numeric inputs that can be coerced to character format.

Usage

n_decimals(x = NULL)

Arguments

x

Numeric (or character) numeric value.

Value

An integer representing the number of decimal places in the input value. If the input value does not have any decimal places, the function returns 0.

Author

Ahmed El-Gabbas

Examples

n_decimals(x = "13.45554545")
#> [1] 8
n_decimals(x = 13.45554545)
#> [1] 8

# -------------------------------------------

# the function ignores trailing zeros for doubles
n_decimals(x = 15.01500)
#> [1] 3

n_decimals(x = '15.01500')
#> [1] 5