编辑:愚蠢的我,链接就在我给的那个上面。使用像这里给出的示例一样的渲染器。
如果您提前知道需要哪些精度,您可以设置一个控制器来处理这些情况,并将它们推送到视图中。
splash 提出了一种可以做到这一点的方法。一种更通用的方式可能类似于以下内容:
class DoublePrecisionGetters {
public double number;
public NumberFormat nf = NumberFormat.getInstance();
public DoublePrecisionGetters(double d)
{ number = d; }
public String getTwoPlaces() {
nf.applyPattern("0.00");
return nf.format(number);
}
public String getThreePlaces() {
nf.applyPattern("0.000");
return nf.format(number);
}
// etc
}
你会像这样设置 ST:
ST template = new ST("two places: $number.twoPlaces$", '$');
template.setAttribute("number", new DoublePrecisionGetters(3.14159));
最后,如果你真的需要它是完全通用的,你可以通过注册一个 ModelAdaptor 来处理双精度数(或其他),并让它根据请求的 propertyName 计算出精度,从而与模型适配器一起破解一些东西。