Using format to align text left and numbers right

@EricGT copied this and changed the owner to @CapelliC (ref)

To pad and align you should use tab stops, controlled by pairs of t and | . For instance, to print a table of numbers in spreadsheet default style (text left align, number right align):

test(indent) :- nl,
    forall(member(L, [[a,    3.66,      55.5334],
              [basd, 22.876345, 2113.4465],
              [cas,  0.6623233, 53.5]
           format('~s~t~20|~t~3f~40|~t~3f~60|~n', L)).

Note the position of ‘space allocator’ specifier ~t , the absolute ‘column width’ ~| , regards the field type specifier. The output:

?- run_tests(sheet_inventory:indent).
% PL-Unit: sheet_inventory:indent 
a                                  3,660              55,533
basd                              22,876            2113,447
cas                                0,662              53,500

Here’s my simplistic answer (quoting myself from stackoverflow and expanding the answer a bit)

~| sets a tab to “here”, ~t inserts fill characters, ~+ advances the tab beyond the last “here” ( ~|) and distributes the fill characters. So,

format("(~|~`.t~d~5+)~n", [123])

produces (..123) – this format string right-justifies the number with . s in a width of 5, surrounded by parentheses.

To left justify, simply move the ~t to where you want the fill characters (after the value):

format("(~|~d~`.t~5+)~n", [123])

which produces (123..). And you can center-justify (.123.) by putting in two ~ts, one before and one after:

format("(~|~`.t~d~`.t~5+)~n", [123])