用stringBuffer进行组装的, 先找出每个参数的位置,一直参数的个数 逐一对结果按位置进行组装代码如下:private StringBuffer subformat(Object[] arguments, StringBuffer result, FieldPosition fp, List characterIterators) { // note: this implementation assumes a fast substring & index. // if this is not true, would be better to append chars one by one. int lastOffset = 0; int last = result.length(); for (int i = 0; i <= maxOffset; ++i) { result.append(pattern.substring(lastOffset, offsets[i])); lastOffset = offsets[i]; int argumentNumber = argumentNumbers[i]; if (arguments == null || argumentNumber >= arguments.length) { result.append("{" + argumentNumber + "}"); continue; } // int argRecursion = ((recursionProtection >> (argumentNumber*2)) & 0x3); if (false) { // if (argRecursion == 3){ // prevent loop!!! result.append('\uFFFD'); } else { Object obj = arguments[argumentNumber]; String arg = null; Format subFormatter = null; if (obj == null) { arg = "null"; } else if (formats[i] != null) { subFormatter = formats[i]; if (subFormatter instanceof ChoiceFormat) { arg = formats[i].format(obj); if (arg.indexOf('{') >= 0) { subFormatter = new MessageFormat(arg, locale); obj = arguments; arg = null; } } } else if (obj instanceof Number) { // format number if can subFormatter = NumberFormat.getInstance(locale); } else if (obj instanceof Date) { // format a Date if can subFormatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, locale);//fix } else if (obj instanceof String) { arg = (String) obj; } else { arg = obj.toString(); if (arg == null) arg = "null"; } // At this point we are in two states, either subFormatter // is non-null indicating we should format obj using it, // or arg is non-null and we should use it as the value. if (characterIterators != null) { // If characterIterators is non-null, it indicates we need // to get the CharacterIterator from the child formatter. if (last != result.length()) { characterIterators.add( createAttributedCharacterIterator(result.substring (last))); last = result.length(); } if (subFormatter != null) { AttributedCharacterIterator subIterator = subFormatter.formatToCharacterIterator(obj); append(result, subIterator); if (last != result.length()) { characterIterators.add( createAttributedCharacterIterator( subIterator, Field.ARGUMENT, new Integer(argumentNumber))); last = result.length(); } arg = null; } if (arg != null && arg.length() > 0) { result.append(arg); characterIterators.add( createAttributedCharacterIterator( arg, Field.ARGUMENT, new Integer(argumentNumber))); last = result.length(); } } else { if (subFormatter != null) { arg = subFormatter.format(obj); } last = result.length(); result.append(arg); if (i == 0 && fp != null && Field.ARGUMENT.equals( fp.getFieldAttribute())) { fp.setBeginIndex(last); fp.setEndIndex(result.length()); } last = result.length(); } } } result.append(pattern.substring(lastOffset, pattern.length())); if (characterIterators != null && last != result.length()) { characterIterators.add(createAttributedCharacterIterator( result.substring(last))); } return result; }
谁来回答下啊
MessageFormat mf = new MessageFormat("数字{0,number,#.##}");
System.out.println(mf.format(new Object[] { 12345.222 }));//去掉逗号,保留两位小数
float dblNum = 10f;
nf.setMaximumFractionDigits(2);
先找出每个参数的位置,一直参数的个数
逐一对结果按位置进行组装代码如下:private StringBuffer subformat(Object[] arguments, StringBuffer result,
FieldPosition fp, List characterIterators) {
// note: this implementation assumes a fast substring & index.
// if this is not true, would be better to append chars one by one.
int lastOffset = 0;
int last = result.length();
for (int i = 0; i <= maxOffset; ++i) {
result.append(pattern.substring(lastOffset, offsets[i]));
lastOffset = offsets[i];
int argumentNumber = argumentNumbers[i];
if (arguments == null || argumentNumber >= arguments.length) {
result.append("{" + argumentNumber + "}");
continue;
}
// int argRecursion = ((recursionProtection >> (argumentNumber*2)) & 0x3);
if (false) { // if (argRecursion == 3){
// prevent loop!!!
result.append('\uFFFD');
} else {
Object obj = arguments[argumentNumber];
String arg = null;
Format subFormatter = null;
if (obj == null) {
arg = "null";
} else if (formats[i] != null) {
subFormatter = formats[i];
if (subFormatter instanceof ChoiceFormat) {
arg = formats[i].format(obj);
if (arg.indexOf('{') >= 0) {
subFormatter = new MessageFormat(arg, locale);
obj = arguments;
arg = null;
}
}
} else if (obj instanceof Number) {
// format number if can
subFormatter = NumberFormat.getInstance(locale);
} else if (obj instanceof Date) {
// format a Date if can
subFormatter = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, locale);//fix
} else if (obj instanceof String) {
arg = (String) obj; } else {
arg = obj.toString();
if (arg == null) arg = "null";
} // At this point we are in two states, either subFormatter
// is non-null indicating we should format obj using it,
// or arg is non-null and we should use it as the value. if (characterIterators != null) {
// If characterIterators is non-null, it indicates we need
// to get the CharacterIterator from the child formatter.
if (last != result.length()) {
characterIterators.add(
createAttributedCharacterIterator(result.substring
(last)));
last = result.length();
}
if (subFormatter != null) {
AttributedCharacterIterator subIterator =
subFormatter.formatToCharacterIterator(obj); append(result, subIterator);
if (last != result.length()) {
characterIterators.add(
createAttributedCharacterIterator(
subIterator, Field.ARGUMENT,
new Integer(argumentNumber)));
last = result.length();
}
arg = null;
}
if (arg != null && arg.length() > 0) {
result.append(arg);
characterIterators.add(
createAttributedCharacterIterator(
arg, Field.ARGUMENT,
new Integer(argumentNumber)));
last = result.length();
}
}
else {
if (subFormatter != null) {
arg = subFormatter.format(obj);
}
last = result.length();
result.append(arg);
if (i == 0 && fp != null && Field.ARGUMENT.equals(
fp.getFieldAttribute())) {
fp.setBeginIndex(last);
fp.setEndIndex(result.length());
}
last = result.length();
}
}
}
result.append(pattern.substring(lastOffset, pattern.length()));
if (characterIterators != null && last != result.length()) {
characterIterators.add(createAttributedCharacterIterator(
result.substring(last)));
}
return result;
}