Java - Justificado variable printf

 
Vista:

Justificado variable printf

Publicado por Pablo (3 intervenciones) el 27/11/2009 01:19:03
Alguien sabe como justificar con printf o cualquier otro metodo, de forma variable?
es decir , en vez de:
System.out.printf("|%6d|%12s",i,s);
y tener 1er columna de 6 y segunda de 12, pasarle esos valores variables\
Gracias!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Justificado variable printf

Publicado por Tom (1831 intervenciones) el 27/11/2009 10:28:09
Viene en el manual de printf() (sólo hay que leerlo):

"
The field width
An optional decimal digit string (with non-zero first digit) specifying a minimum field width. If the converted value has fewer
characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been
given). Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the field
width is given in the next argument, or in the m-th argument, respectively, which must be of type int. A negative field width
is taken as a '-' flag followed by a positive field width. In no case does a nonexistent or small field width cause truncation
of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.
"
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Justificado variable printf

Publicado por Tom (1831 intervenciones) el 27/11/2009 10:38:43
Vale, estamos en el foro de Java y no en el de C, joé ... el printf de Java es prácticamente igual que el de C.

Y aún, podrías hacer, con un poco de imaginación :

int width1 = 4;
int width2 = 7;

...
int val1 = 32;
int val2 = 15;

out.printf("%" + width1 +"d ; %" + width2 + "d\n", val1, val2);
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Justificado variable printf

Publicado por Pablo (3 intervenciones) el 28/11/2009 13:14:01
Gracias!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar