Vim Tips Wiki
m (Aligning numbers at decimal-point moved to Align numbers at decimal point: Page moved by JohnBot to improve title)
(Change <tt> to <code>, perhaps also minor tweak.)
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
 
{{Duplicate|139|894|570}}
 
{{Duplicate|139|894|570}}
  +
{{TipImported
{{Tip
 
 
|id=893
 
|id=893
  +
|previous=892
|title=Aligning numbers at decimal-point
 
  +
|next=894
|created=March 10, 2005 6:37
+
|created=2005
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Michael Fitz
 
|author=Michael Fitz
 
|version=5.7
 
|version=5.7
 
|rating=3/5
 
|rating=3/5
  +
|category1=Usage
|text=
 
  +
|category2=
To see the _really_ alignment you should use a fixedsized font (eg. FixedSys on Windows) or mark this all and copy it into vim!
 
 
}}
 
Suppose, you've gotten some numbers in a ugly format:
+
Suppose you have some numbers in an ugly format:
 
<pre>
 
<pre>
---BEG
 
 
123
 
123
 
2.5678
 
2.5678
Line 20: Line 20:
 
100.5
 
100.5
 
+47.11
 
+47.11
---END
 
 
</pre>
 
</pre>
   
You want to have them nice aligned with 5 decimals written out.
+
You want to have them nice aligned with 5 decimals written out.
  +
First, we align any line left to the beginning:
+
First, we align any line left to the beginning:
   
 
<pre>
 
<pre>
 
:%s§^\s*§§
 
:%s§^\s*§§
   
---BEG
 
 
123
 
123
 
2.5678
 
2.5678
Line 35: Line 34:
 
100.5
 
100.5
 
+47.11
 
+47.11
---END
 
 
</pre>
 
</pre>
Now we split at the decimal-point (if any) and shift the fractional part wide to the right and add five '0' at the end (because we want 5 fractional digts).
 
   
 
Now we split at the decimal-point (if any) and shift the fractional part wide to the right and add five '0' at the end (because we want 5 fractional digts).
(The decimal-point has been replaced by '!' for simpleness: you are not forced to escape '!' :-)
 
 
<pre>
 
<pre>
 
:%s§\([-+]\?\d\+\)\.\?\(\d*$\)§\1 !\200000§
 
:%s§\([-+]\?\d\+\)\.\?\(\d*$\)§\1 !\200000§
   
---BEG
 
 
123 !00000
 
123 !00000
 
2 !567800000
 
2 !567800000
Line 49: Line 45:
 
100 !500000
 
100 !500000
 
+47 !1100000
 
+47 !1100000
---END
 
 
</pre>
 
</pre>
  +
This tricky substitue aligns the fractional part at column 15:
+
This tricky substitue aligns the fractional part at column 15:
 
<pre>
 
<pre>
 
:%s§\%15c\s*!§!§
 
:%s§\%15c\s*!§!§
   
---BEG
 
 
123 !00000
 
123 !00000
 
2 !567800000
 
2 !567800000
Line 61: Line 56:
 
100 !500000
 
100 !500000
 
+47 !1100000
 
+47 !1100000
---END
 
 
</pre>
 
</pre>
  +
Now we shift the integral part back by exchanging it with leading spaces (and by the mean replacing '!' by decimal-point):
+
Now we shift the integral part back by exchanging it with leading spaces (and replacing '!' by decimal-point):
 
<pre>
 
<pre>
 
:%s§\(^\S*\)\(\s*\)!§\2\1.§
 
:%s§\(^\S*\)\(\s*\)!§\2\1.§
   
---BEG
 
 
123.00000
 
123.00000
 
2.567800000
 
2.567800000
Line 73: Line 67:
 
100.500000
 
100.500000
 
+47.1100000
 
+47.1100000
---END
 
 
</pre>
 
</pre>
  +
Now we truncate each fractional part to 5 digits:
+
Now we truncate each fractional part to 5 digits:
 
<pre>
 
<pre>
 
:%s§\%21c\d*§§
 
:%s§\%21c\d*§§
  +
 
---BEG
 
 
123.00000
 
123.00000
 
2.56780
 
2.56780
Line 85: Line 78:
 
100.50000
 
100.50000
 
+47.11000
 
+47.11000
---END
 
 
</pre>
 
</pre>
  +
Finally we add a '+'-sign where it's missing:
+
Finally we add a '+'-sign where it's missing:
 
<pre>
 
<pre>
 
:%s§\s\(\d\)§+\1§
 
:%s§\s\(\d\)§+\1§
   
 
---BEG
 
 
+123.00000
 
+123.00000
 
+2.56780
 
+2.56780
Line 98: Line 89:
 
+100.50000
 
+100.50000
 
+47.11000
 
+47.11000
---END
 
 
</pre>
 
</pre>
Looks now very nice, doesn't it?
 
   
 
I usually use the (german) paragraph-sign '§' to surround the substitute-patterns, because this letter is very seldom used in any IT-related context.
   
 
==Comments==
}}
 
 
See [[VimTip139]] for AlignMaps which provides the <code>\anum</code> (actually <Leader>anum) to do something similar:
 
== Comments ==
 
Addendum to this tip:
 
I usually use the (german) paragraph-sign '§' to surround the substitute-patterns, because this letter is very seldom used in any IT-related context ;-)
 
 
'''Anonymous'''
 
, March 10, 2005 6:40
 
----
 
FYI: AlignMaps provides the \anum (actually &lt;Leader&gt;anum) to do something similar:
 
 
<pre>
 
<pre>
 
123
 
123
Line 118: Line 100:
 
-13.44
 
-13.44
 
100.5
 
100.5
+47.11
+
+47.11
 
</pre>
 
</pre>
It doesn't append the zeros, just does a numeric alignment. \anum also handles the use of commas instead of periods, European style.
 
   
 
It doesn't append the zeros, just does a numeric alignment. <code>\anum</code> also handles the use of commas instead of periods, European style.
   
NdrOchip--AT--ScampbellPfamily.AbizM - NOSPAM
 
, March 10, 2005 12:50
 
 
----
 
----
<!-- parsed by vimtips.py in 0.530612 seconds-->
 
[[Category:Usage]]
 

Revision as of 05:54, 13 July 2012

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 893 Printable Monobook Previous Next

created 2005 · complexity intermediate · author Michael Fitz · version 5.7


Suppose you have some numbers in an ugly format:

123
2.5678
-13.44
100.5
  +47.11

You want to have them nice aligned with 5 decimals written out.

First, we align any line left to the beginning:

:%s§^\s*§§

123
2.5678
-13.44
100.5
+47.11

Now we split at the decimal-point (if any) and shift the fractional part wide to the right and add five '0' at the end (because we want 5 fractional digts).

:%s§\([-+]\?\d\+\)\.\?\(\d*$\)§\1                        !\200000§

123                        !00000
2                        !567800000
-13                        !4400000
100                        !500000
+47                        !1100000

This tricky substitue aligns the fractional part at column 15:

:%s§\%15c\s*!§!§

123           !00000
2             !567800000
-13           !4400000
100           !500000
+47           !1100000

Now we shift the integral part back by exchanging it with leading spaces (and replacing '!' by decimal-point):

:%s§\(^\S*\)\(\s*\)!§\2\1.§

           123.00000
             2.567800000
           -13.4400000
           100.500000
           +47.1100000

Now we truncate each fractional part to 5 digits:

:%s§\%21c\d*§§

           123.00000
             2.56780
           -13.44000
           100.50000
           +47.11000

Finally we add a '+'-sign where it's missing:

:%s§\s\(\d\)§+\1§

          +123.00000
            +2.56780
           -13.44000
          +100.50000
           +47.11000

I usually use the (german) paragraph-sign '§' to surround the substitute-patterns, because this letter is very seldom used in any IT-related context.

Comments

See VimTip139 for AlignMaps which provides the \anum (actually <Leader>anum) to do something similar:

123
  2.5678
-13.44
100.5
+47.11

It doesn't append the zeros, just does a numeric alignment. \anum also handles the use of commas instead of periods, European style.