Is there any possibility to define a format for java.text.MessageFormat that formats numbers without grouping separators and other objects with .toString()?
My trouble is that I have to format an argument for which I don't in advance whether it is a number or not. If I just use
java.text.MessageFormat.format("The object: {0}",someobject)
that usually just calls someobject.toString()
as I want, but when someobject
is actually a number 1231231
, it returns (in the German locale) The object: 1,231,231
, but I want The object: 1231231
here. Formatting with
java.text.MessageFormat.format("{0,number,#}",1231231)
returns The object: 1231231
, but this doesn't work when the argument is not a number. Is there a way to specify a format that works for both numbers and other objects?
(In case your wondering: unfortunately, changing the locale of the MessageFormat is not an option, since that's deeply buried in JBoss' logging framework and inaccessible.)
UPDATE: calling String.valueOf(someobject) before passing it as an argument does solve this problem, but also isn't feasible in my case, since this problem occurs when logging, and I don't want to call the potentially expensive someobject.toString() when the logging level doesn't allow the log message and the MessageFormat is thus never applied.