formatting - In Java, is there a way to format the output of a double/int or non-string variable? -
i realize sounds simple, , is... code within main(), inside of loop:
system.out.println(num[i]+"\t "+qty[i]+"\t "+money.format(price[i])+"\t"+money.format(value[i])+"\t"+reorder[i]); with total captured here:
http://maradastudios.ucoz.com/school/capture.png
as may have noticed, works fine. however, during output line of #114 (2nd last line) has total value of $90.00. correct, causes odd spacing reorder point variable. state simply, can format variable take same amount of space it's larger-digited counterparts?
something like
string.format("%10.2f", yourfloat) // or system.out.format("%10.2f", yourfloat) will print 10-character wide (including decimal) string, 2 numeric characters after decimal.
(docs)
so
string.format("$%6.2f", value[i]) will align both $ , . characters (unless value[i] > 999.99).
instead of:
system.out.println( num[i] +"\t "+ qty[i] +"\t "+ money.format(price[i])+"\t"+ money.format(value[i])+"\t"+ reorder[i]); (which had, formatted clarity , remove scroll bar)
i'd write:
system.out.format("%5d\t %5d\t $%5.2f\t $%6.2f\t %5d %n", num[i], qty[i], price[i], value[i], reorder[i]); this assumes price , value arrays floats or doubles. since money isn't standard class, it's hard tell other add $ sign.
the string format syntax defined in docs, floats it's roughly:
%x.yf where x total field width , y number of decimal points
for example
"123.40" has total width of 6: 3 + 1 [decimal point] + 2 = 6) " 2.34" has total width of 6: 2 [spaces] + 1 + 1 [decimal point] + 2 = 6
Comments
Post a Comment