This example show you how to use java.text.MessageFormat
class to format a message that contains numbers.
package org.kodejava.text;
import java.text.MessageFormat;
import java.util.Locale;
public class MessageFormatNumber {
public static void main(String[] args) {
// Set the Locale for the MessageFormat.
Locale.setDefault(Locale.US);
// Use the default formatting for number.
String message = MessageFormat.format("This is a {0} and {1} numbers",
10, 75);
System.out.println(message);
// This line has the same format as above.
message = MessageFormat.format("This is a {0,number} and {1,number} " +
"numbers", 10, 75);
System.out.println(message);
// Format a number with 2 decimal digits.
message = MessageFormat.format("This is a formatted {0, number,#.##} " +
"and {1, number,#.##} numbers", 25.7575, 75.2525);
System.out.println(message);
// Format a number as currency.
message = MessageFormat.format("This is a formatted currency " +
"{0,number,currency} and {1,number,currency} numbers",
25.7575, 25.7575);
System.out.println(message);
// Format numbers in percentage.
message = MessageFormat.format("This is a formatted percentage " +
"{0,number,percent} and {1,number,percent} numbers", 0.10, 0.75);
System.out.println(message);
}
}
The result of the program are the following lines:
This is a 10 and 75 numbers
This is a 10 and 75 numbers
This is a formatted 25.76 and 75.25 numbers
This is a formatted currency $25.76 and $25.76 numbers
This is a formatted percentage 10% and 75% numbers
Latest posts by Wayan (see all)
- How do I split large excel file into multiple smaller files? - April 15, 2023
- How do I get the number of processors available to the JVM? - March 29, 2023
- How do I show Spring transaction in log / console? - March 29, 2023