【发布时间】:2018-03-29 22:24:54
【问题描述】:
我有:
${amount?string.currency}
它很好地格式化了我的 BigDecimal,唯一的是它包含了我不想要的货币符号(美元符号)。如何在不使用 string["0.##"] 明确指定数字格式的情况下禁用此功能?
【问题讨论】:
标签: java freemarker
我有:
${amount?string.currency}
它很好地格式化了我的 BigDecimal,唯一的是它包含了我不想要的货币符号(美元符号)。如何在不使用 string["0.##"] 明确指定数字格式的情况下禁用此功能?
【问题讨论】:
标签: java freemarker
目前 (2.3.27) ?string.currency 始终表示 Java 提供的默认货币格式。因此,无需更改它,您可以定义自定义格式并像 amount?string.@currency 一样使用它(其中 currency 只是您为格式指定的名称)。
自定义格式在 Java 中定义。来自手册 (http://freemarker.org/docs/pgui_config_custom_formats.html#pgui_config_custom_formats_ex_alias):
// Where you initalize the application-wide Configuration singleton:
Configuration cfg = ...;
Map<String, TemplateNumberFormatFactory> customNumberFormats = new HashMap<>();
customNumberFormats.put("price",
new AliasTemplateNumberFormatFactory(",000.00"));
customNumberFormats.put("weight",
new AliasTemplateNumberFormatFactory("0.##;; roundingMode=halfUp"));
cfg.setCustomNumberFormats(customNumberFormats);
然后在模板中:
${product.price?string.@price}
${product.weight?string.@weight}
【讨论】:
如果你只是想要格式使用
${amount?string["0.##"]}
或设置number_format:
<#setting number_format="0.##">
查看所有 freemarker formats options
【讨论】: