MessageFormat format Example
MessageFormat class format example. This example shows you how to use format method.
MessageFormat class format example. public final StringBuffer format(Object[] ,StringBuffer ,FieldPosition ) Formats an array of objects and appends the MessageFormat's pattern, with format elements replaced by the formatted objects, to the provided StringBuffer.
Here is the code
/*
* @ # Format.java
* A class representing 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 Format {
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 |
|