2

I tried to use the arithmetic register with a fraction (7/8) and it returned 0! How do I exert better control over the register so that it gives me what I want?

The literal sequence I used, while in insert mode: <C-R>=(7/8)<enter> result: 0

5

As none of your numbers contains a decimal point (:help floating-point-format), Vim treats the expression as integer division, and also gives an integer result.

Simply specify one or both numbers as floats, and you'll get the expected result: <C-R>=7.0/8<CR> gives 0.875.

For more control (number of digits, precision, padding), use printf() with the corresponding format specifications.

2
  • 1
    Thanks. I had tried =(7./8) suspecting I had to force vim to recognize it as a float but didn't get as far as trying .0 before giving up. Appreciate your answer and thanks for the reference to the relevant help page (which I also couldn't find on my own).
    – Harv
    Dec 5 '19 at 10:03
  • 1
    Glad I could help! Vim's syntax is a bit unintuitive there, as float support had to be introduced into the existing syntax to avoid breaking backwards compatibility, especially in the area of string concatenation via .. Dec 5 '19 at 10:14

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.