PRINTF Syntax for centering a string

I am using PRINTF to format some output.  I know it is possible to justify left and right:

printf '%9s',   "test";   # prints "     test"
printf '%-6s',  "test";   # prints "test     "

My question:  how can I center the string?    i.e. "   test   "

cs76737Asked:
Who is Participating?
 
Adam314Connect With a Mentor Commented:
there is no format specifier available in printf to center.
0
 
TalmashCommented:
my $string = "test";
$len = len($string);
my $range = 20;
my $space = int((20-$len)/2);
print " " x $space.$string." " x $space;
0
 
cs76737Author Commented:
Talmash --

thank you for the suggestion.  Again, I need to do this within the PRINTF function for my program to work.  

Is there a way to do it using PRINTF?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.