Right justified numbers in Perl

How can I print the numbers justified in Perl, for example:

a= 1 b= 22 c= 333 d=4444 
+4
source share
3 answers

Try it.

 printf ("%4d\n",1); printf ("%4d\n",11); printf ("%4d\n",111); printf ("%4d\n",1111); 
+11
source

The official resource for this is perldoc -f sprintf , which has a good summary of examples:

For instance:

  printf '<% d>', 12; # prints "< 12>" printf '<%+d>', 12; # prints "<+12>" printf '<%6s>', 12; # prints "< 12>" printf '<%-6s>', 12; # prints "<12 >" printf '<%06s>', 12; # prints "<000012>" 
+4
source

Use printf with precision and a space like "filler":

 printf "a=% 4d\n", 1; printf "b=% 4d\n", 22; 
+2
source

Source: https://habr.com/ru/post/1389408/


All Articles