Java - How to format a mix of strings and integers using printf() to get equal length in the output? -


i having issues formatting correctly:

       starting       mechanical cuchoo clock time [ 0:00:00], total drift = 0.00 seconds     after 1 day      mechanical cuchoo clock time [23:59:00], total drift = 60.00 seconds 

the correct formatting is:

   starting  mechanical cuckoo clock time [ 0:00:00], total drift = 0.00 seconds after 1 day  mechanical cuckoo clock time [23:59:00], total drift = 60.00 seconds 

i tried , works there better way?

system.out.printf("%60s", this.getclocktype() + " cuchoo clock time [" + time.formattedreportedtime() +             "], " + "total drift = ");  system.out.printf("%s", fmt.format(time.gettotaldrift()) + "\n"); 

here small snippet in addition answer caio.

string format =      "%12s  mechanical cuckoo clock time [%8s], total drift = %5.2f seconds%n"; system.out.printf(format, "starting", "0:00:00", 0.0); system.out.printf(format, "after 1 day", "23:59:00", 60.0); 

output

   starting  mechanical cuckoo clock time [ 0:00:00], total drift =  0.00 seconds after 1 day  mechanical cuckoo clock time [23:59:00], total drift = 60.00 seconds 
  • %12s - (first) parameter formatted left padded string 12 characters width
  • %8s - above, (second) parameter 8 characters
  • %5.2f - (third) parameter, must floating point type, formated width of 5 charcaters , precision of 2

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -