Search code examples
javamessageformat

How to configure MessageFormat for numbers globaly


I would like to change the way MessageFormat prints out numbers.

Integer foo = 99888;
String bar = MessageFormat.format("{0}", foo);

Observed value is "99,888".

Desired value is "99888".

What I could do is this:

String bar = MessageFormat.format("{0, number,#}", foo);

The problem is that I have to change it in a project with more than 10'000 MessageFormat.format(). So this is not really the way I would like to do it.

Do you know if there is a way to change the format of numbers given to MessageFormat.format() globally?


Solution

  • Other than writing a very strange aspect I don't believe there is a way. That's the price you pay for using a static method that you can't modify.

    I'd look into doing a bulk change either using your favourite IDE or command line tools e.g. sed. If you want to change all occurrences of {0} into {0,number,#} it should be enough to

    sed -i -e 's/{0}/{0,number,#}/g' MyClass.java