Counts the significant decimal places in a numeric value, ignoring trailing
zeros. Used to derive coordinate precision from supplied latitude and longitude
values, which in turn feeds coordinateUncertainty.
Value
An integer vector giving the number of significant decimal places for
each element of x. NA elements return NA.
Details
The value is converted with as.character(), everything up to and including the
decimal point is dropped, trailing zeros are stripped, and the remaining
characters are counted. Three consequences are worth knowing:
Whole numbers return
0, because there are no decimals to count.The sign is ignored, since the minus sign is removed along with the integer part.
Values that
as.character()renders in scientific notation return0.1e-05becomes the string"1e-05", which contains no decimal point, so the result is0rather than5. Convert such values withformat()first if you need the true precision.
NA input returns NA. The function is vectorised over x.
Examples
# Typical decimals
get_decimal_places(1.25)
#> [1] 2
# Whole numbers have no decimal places
get_decimal_places(100)
#> [1] 0
# The sign is ignored
get_decimal_places(-1.5)
#> [1] 1
# Trailing zeros do not count
get_decimal_places(1.10)
#> [1] 1
# Vectorised over the input
get_decimal_places(c(1.5, 2.25, 3))
#> [1] 1 2 0