java - How do I use the printf function while keeping columns organized? -


i'm trying print out invoice report in java project having trouble formatting sheet. code i'm having problems with.

stringbuilder sb = new stringbuilder();  sb.append(string.format("%s%s%-82s%s%10.2f%s","discount ( ","placeholder"," )", "$", 99.99, "\n")); 

which prints out this

discount ( placeholder )                                                                                $     99.99 

if change character length of word "placeholder," $99.99 column moved, such.

discount ( p )                                                                                $     99.99 

what need code whether word "placeholder" replaced word 10 characters long or 1 character long, $99.99 needs stay in same place in correct column when print. tried modifying %s $ seems still move if placeholder changed. advice?

you try width parameter.
(15 width here):

system.out.printf("%s%15s%-82s%s%10.2f%s", "discount ( ", "placeholder", " )", "$", 99.99, "\n"); 

not sure if string.format uses same syntax printf though.

the output be:

discount (     placeholder )                                           $     99,99 discount (               p )                                           $     99,99 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -