Hi,
Thank you for your reply.
In a first moment I thought that it was a good solution, but I have also a lot of already existing patterns where is specified the number of decimals (and it is not the same for all pattern) in this way:
{0,number,0.00}
If I adopt your solution, it throws an IllegalArgumentException, because it wait for a number where there is a string.
I also tried to set all formats for all numeric elements, but the DecimalFormat is stronger than the formatting pattern. Ie:
messageFormat.applyPattern("{0} - {0,number,0.00}");
return messageFormat(new Decimal(12345.6789));
returns
12,345.679 - 12,345.68
that is not that I want (because the thosuand grouping).
But:
Object[] params = new Object[] {new Double(12345.6789)};
Format[] fmts = createNumberFormatOnNumericParameters(params);
messageFormat.applyPattern("{0} - {0,number,0.00}");
messageFormat.setFormatsByArgumentIndex(fmts);
return messageFormat(new Decimal(12345.6789));
private Format[] createNumberFormatOnNumericParameters(Object[] params) {
Format[] result = Format[params.length];
Format numFormat = NumberFormat.getInstance();
numFormat.setGroupingUsed(false);
for (int i = 0; i < params.length; i++ ) {
if (params[i] instanceOf Number) result[i] = numFormat;
return result;
returns
12345.679 - 12345.679
that is not what I want (because the number of decimals).
Note: I cannot use the setFormat of setFormats until I can't know the types of the arguments that the parameters refer to instead the argument types.
For this moment, I adpoted your solution (I didn't found any already wrote pattern where there is both formatted and unformatted "thousanded" numbers for now), in this way:
try {
return formatText(pattern, params, true);
} catch (IllegalArgumentException e) {
return formatText(pattern, params, false);
}
where the formatText method implements optionally your solution...