How can I print numbers right justified in Perl, like this:
a= 1
b= 22
c= 333
d=4444
Try like this.
printf ("%4d\n",1);
printf ("%4d\n",11);
printf ("%4d\n",111);
printf ("%4d\n",1111);
The official resource for this is perldoc -f sprintf
, which has a nice summary of examples:
For example:
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>"
Use printf
with a precision and a space as "filler":
printf "a=% 4d\n", 1;
printf "b=% 4d\n", 22;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With