Now it's time to start taking a closer look at the different types of variables you can use. First we'll go over Vim's numeric types.
Vimscript has two types of numeric variables: Numbers and Floats. A Number is a 32 bit signed integer. A Float is a floating point number.
You can specify Numbers in a few different ways. Run the following command:
No surprises here -- Vim displays "100". Now run this command:
This time Vim displays "255". You can specify numbers in hex notation by
prefixing them with
0X. Now run this command:
You can also use octal by starting a number with a
0. Be careful with this,
because it's easy to make mistakes. Try the following commands:
:echom 017 :echom 019
Vim will print "15" for the first command, because "17" in octal is equal to
"15" in decimal. For the second command Vim treats it as a decimal number, even
though it starts with a
0, because it's not a valid octal number.
Because Vim silently does the wrong thing in this case, I'd recommend avoiding the use of octal numbers when possible.
Floats can also be specified in multiple ways. Run the following command:
Notice that we're using
echo here and not
echom like we usually to. We'll
talk about why in a moment.
Vim displays "100.1" as expected. You can also use exponential notation. Run this command:
Vim displays "5450.0". A negative exponent can also be used. Run this command:
Vim displays "0.1545". The
- before the power of ten is optional. If
it's omitted then it's assumed to be positive. Run the following command:
Vim will display "1.53e10", which is equivalent. The decimal point and number after it are not optional. Run the following command and see that it crashes:
When you combine a Number and a Float through arithmetic, comparison, or any other operation Vim will cast the Number to a Float, resulting in a Float. Run the following command:
:echo 2 * 2.0
Vim displays "4.0".
When dividing two Numbers, the remainder is dropped. Run the following command:
:echo 3 / 2
Vim displays "1". If you want Vim to perform floating point division one of the numbers needs to be a Float, which will cause the other one to be coerced to a Float as well. Run this command:
:echo 3 / 2.0
Vim displays "1.5". The "3" is coerced to a Float, and then normal floating point division is performed.
:help Float. When might floating point number not work in Vimscript?
:help floating-point-precision. What might this mean if you're writing
a Vim plugin that deals with floating point numbers?