MessageFormat format Example
MessageFormat class format example. This example shows you how to use format method.
MessageFormat class format example. ublic static String format(String pattern, Object... arguments) Creates a MessageFormat with the given pattern and uses it to format the given arguments. This is equivalent to
Here is the code
/*
* @ # Format1.java
* A class repersenting use to Format method
* of MessageFormat class in java.text package
* version 17 June 2008
* author Rose India
*/
import java.text.*;
import java.util.*;
import java.math.*;
public class Format1 {
public static void main(String args[]) {
String pattern = ". I bought {0,number,#} apple";
java.text.Format formats[] = {new DecimalFormat()};
//Format[] formats ={new DecimalFormat("#")};
Object[] values = {new Integer(500)};
StringBuffer buffer = new StringBuffer(24);
BigDecimal bd1 = new BigDecimal(1);
FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
NumberFormat numForm = NumberFormat.getInstance();
buffer = numForm.format(bd1, buffer, pos);
MessageFormat mFmt = new MessageFormat(pattern);
mFmt.setFormats(formats);
mFmt.applyPattern(pattern);
System.out.println(mFmt.toPattern());
System.out.println(mFmt.format(values, buffer, pos));
}
} |
Output
. I bought {0,number,#} apple
1. I bought 500 apple |
|