如何使用FreeMarker中的总货币?

时间:2017-08-04 05:12:52

标签: java freemarker

如何在保持精度和not losing cents due to double of float reprensations的同时在FreeMarker中制表数据?

<#assign totalSalaries = 0>

<#list employees as employee>
    <#assign totalSalaries = totalSalaries + emplpoyee.salary>
</#list>

${totalSalaries?string.currency}

就我个人而言,我更喜欢使用多头并将所有内容保留在美分中,但我找不到一个简单的解决方案来将其转换为货币值。无论哪种方式,如果我使用longBigDecimal(它很容易通过美分转换为BigDecimal),我怎样才能保证打印出来的总工资赢得了&#39;由于双打或漂浮的精确错误,会失去任何美分?换句话说,FreeMarker如何确定将变量总数定义为什么类型?

1 个答案:

答案 0 :(得分:2)

FreeMarker does calculations (and the numerical type conversions needed as part of that) with the ArithmeticEngine, which is a configuration setting. The default of that setting is BigDecimalEngine, which converts everything to BigDecimal before doing anything with them, and keeps the results as BigDecimal as well. So at least with the default you won't lose any digits.