Use MessageFormat.format , you can also specify formatting arguments in replacement tokens.
message = MessageFormat.format("This is a formatted percentage " + "{0,number,percent} and a string {1}", varNumber, varText); System.out.println(message); message = MessageFormat.format("This is a formatted {0, number,#.##} " + "and {1, number,#.##} numbers", 25.7575, 75.2525); System.out.println(message);
Alternatively, you can use String.format , but this does not guarantee a position, for example. String.format("What do you get if you multiply %d by %s?", varNumber, varText); .
source share