Remember C and sprintf? It was a long time ago:) That was very useful function, so here is Java version (enhanced a bit). It is easy to use and fast. Formatting is specified in a string, as in C. And yes, Java from version 5 has its own implementation, but Jodd version may be still interesting.
Printf formats a number in a printf format, like C.
The format string has a prefix, a format code and a suffix. The prefix and suffix
become part of the formatted output. The format code directs the
formatting of the (single) parameter to be formatted. The code has the
following structure:
Examples:
Printf.str("%+i", 173); // +173
Printf.str("%04d", 1); // 0001
Printf.str("%f", 1.7); // 1.700000
Printf.str("%1.1f", 1.7); // 1.7
Printf.str("%.4e", 100.1e10); // 1.0010e+012
Printf.str("%G", 1.1e13); // 1.1E+013
Printf.str("%l", true); // true
Printf.str("%L", 123); // TRUE
Printf.str("%b", 13); // 1101
Printf.str("%,b", -13); // 11111111 11111111 11111111 11110011
Printf.str("%#X", 173); // 0XAD
Printf.str("%,x", -1); // ffff ffff
Printf.str("%s %s", new String[]{"one", "two"}); // one two
Note that float-point values are not precise, so the printed value may differ from argument.