Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!

I have to use a MessageFormat without knowing the elements (the pattern comes from a user input), but I want that all the numbers would be without the thousand separator.

I tried to search in all available locales, but all of them have the thousand separator.
I tried to create a new Locale, but I wasn't able to define a numberformat for him.
I tried to set ALL elements with a new DecimalFormat having the GroupingUsed setted to false, but the first element found that is a non numeric element, an exception was thrown.

What I have to do?
The unique solution I found is to try to set all parameters trying to format the message after each of that. If an error is catched, I have to set as nothing that format, but I think it's a very inelegant solution.
Posted

Have you tried to clone your Locale, get the Numberformat from this and set the GroupingUsed(false) there?
An inelegant solution would also to format the numbers outside the Messageformat if there are Numbers:

Java
Object parm = ...
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);

if (parm instanceof Number) {
   parm = nf.format(parm);
}

System.out.println(MessageFormat.format("hello {0}", parm));
 
Share this answer
 
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:
Java
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:
Java
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:
Java
try {
    return formatText(pattern, params, true);
} catch (IllegalArgumentException e) {
    return formatText(pattern, params, false);
}

where the formatText method implements optionally your solution...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900