Java: utilizar la clase Formatter. Ejemplos de uso esperado:
StringBuilder sb = new StringBuilder();
// Send all output to the Appendable object sb
Formatter formatter = new Formatter(sb, Locale.US);
// Explicit argument indices may be used to re-order output.
formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")
// -> " d c b a"
// Optional locale as the first argument can be used to get
// locale-specific formatting of numbers. The precision and width can be
// given to round and align the value.
formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);
// -> "e = +2,7183"
// The '(' numeric flag may be used to format negative numbers with
// parentheses rather than a minus sign. Group separators are
// automatically inserted.
formatter.format("Amount gained or lost since last statement: $ %(,.2f",
balanceDelta);
// -> "Amount gained or lost since last statement: $ (6,217.58)"
Por favor, no etiquetar un '' printf'-pregunta 'C++. –
@ Space_C0wb0y: no hay nada de malo con el uso de printf en C++. En un mundo ideal, todos usarían iostreams en su C++, pero no vivimos en un mundo ideal. Además, a veces es más fácil obtener una cadena formateada de la manera que uno quiera con printf. – George
En mi opinión, hay mucho error con el uso de 'printf' en C++. C y C++ son dos idiomas completamente diferentes, y las personas hacen suposiciones diferentes sobre qué características de C son correctas en C++ y cuáles no. Diferentes suposiciones siempre conducen a problemas. –