I am using StringTemplate in Java.
I would like to render decimal number with a certain precision (e.g. 3 digits after the decimal point).
Is it possible to for the ST object to do it? And how?
Edit: to clarify, this is especially relevant when rendering objects. E.g. my code looks like
String renderMe(String template, Collection<MyClass> items)
{
// render the items here using the template....
}
renderMe() doesn't have to know anything about the fields of MyClass, in particular it doesn't have to know which fields are floating points. I am looking for a solution that maintains this decoupling.
Register built-in NumberRenderer for Number subclasses and then use format option:
EDIT: stupid me, the link was directly above the one I gave. Use a Renderer like the example given here.
If you know ahead of time which precisions will be needed, you can set up a Controller to handle those cases, and push them into the View.
splash suggested a way that this could be done. A somewhat more generic way might be something like the following:
You would setup ST like:
Finally, if you really need it to be totally generic, you could hack something together with model adaptors by registering a ModelAdaptor to handle doubles (or whatever), and having it figure out the precision based on the requested propertyName.
Not tested, but I guess something like this should work: