I am trying to replace all the float numbers in the string with the same numbers rounded to 2 decimal places. For example "Hello23.898445World1.12212"
should become "Hello23.90World1.12"
.
I may find numbers' positions by gregexpr("[[:digit:]]+\\.*[[:digit:]]*", str)[[1]]
but have no idea how to replace them with their rounded originals.
Or using
stringr
:Solution using no package for the string manipulation:
Also credits to @akrun
format(., nsmall=2)
is the trick in this solution.Input string
Set number of decimal places
Calculate
Result:
We can use
gsubfn
data