3

For example I have many numbers like 111.222, I want to decrease them to 0.1 (to get 111.122), how to do that? I tried:

:%s#\d\+\.\d\+#\=submatch(0)-0.1#

But as a result I have 110.9.

3

In short, you need to convert the string to a Float explicitly, using str2float():

:%s#\d\+\.\d\+#\=str2float(submatch(0))-0.1#

To explain what you're seeing without the explicit conversion, vim has two numeric types: Number (for integers) and Float (for floating point numbers.) When using a string in numeric context (for example, with an arithmetic operation such as -), vim will do an implicit conversion, but it only converts strings to Number, not Float. Trailing characters are ignored in the conversion, so it just stops at ., the decimal point.

The end result is that 111.222 will be converted to the Number 111.

Then, when performing an arithmetic operation between a Number and a Float, vim will first coerce the Number into a Float. So, in your case, it will get to 111.0-0.1, finally yielding 110.9.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.