2012-05-13 40 views
3

我有2個數字我想在freemarker的文件顯示:在Freemarker中,如何爲1行(而不是報表的其餘部分)使用自定義的NumberFormat或DateFormat?

  • 基準long id

  • 基準long timeSpend

的ID應該使用默認NumberFormat實例,但timeSpend應該使用我的MillisecondsSpendNumberFormat實例,所以我在輸出中得到類似這樣的內容:

  • ID:1 234 567

  • timeSpend:1min 23sec 456ms

如何使用我NumberFormat實例1個long實例,但並不是所有的人?

回答

4

您可以將格式化程序實例添加到Freemarker模板(將其添加到包含傳遞給模板的對象的模型中),然後顯式調用其format()方法來格式化數字。

喜歡的東西

timeSpend: ${myCustomFormatter.format(timeSpend)} 
1

您可以使用${someNumber?string("somePattern")}使用非默認格式爲一個單一的場合。但是你真正的問題是FreeMarker總是使用DecimalFormat來進行數字格式化,你只能指定模式。所以你根本不能使用MillisecondsSpendNumberFormat,除非你手動調用它。您可以通過將MillisecondsSpendNumberFormat實例放入數據模型中並從模板中調用它的Java API,或者通過實現在內部進行格式化的TemplateMethodModelEx,然後將其拉入通常的#include -d/#import -d文件中<#assign formatTimeSpent = "com.example.MyTimeSpentFormatter"?new()>,然後在模板中使用${formatTimeSpent(someNumber)}

相關問題