My regex is quite rusty. How could vim be used to change the precision of a decimal number.
For example:
Changing 30.2223221 8188.2121213212 to 30.22 8188.21
Using just VimL:
:%s/\d\+\.\d\+/\=printf('%.2f',str2float(submatch(0)))/g
It's likely possible using vim internal search/replace, but I would use "perldo":
:perldo s/(\d+\.\d+)/sprintf "%.2f", $1/eg
If you just want to truncate the last digit instead of rounding,
:%s/(\d+\.\d\d)\d+/\1/g
Based on the previous answers, using VimL, for negative numbers and exponential notation:
:%s/\c\v[-]=\d+\.\d+(e[+-]\d+)=/\=printf('%.2f',str2float(submatch(0)))/g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With