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 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:
String template =
"foo(x,y) ::= << >>\n";
STGroup g = new STGroupString(template);
g.registerRenderer(Number.class, new NumberRenderer());