0

I have:

 ${amount?string.currency} 

which formats my BigDecimal all nicely, the only thing is that it includes the currency symbol (dollar sign) which I don't want. How can I disable this without explicitely specifying the number format using string["0.##"]?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192

2 Answers2

3

Currently (2.3.27) ?string.currency always means the default currency format provided by Java. So instead of changing that, you could define a custom format and use it like amount?string.@currency (where currency is just a name you have given to the format).

Custom formats are defined in Java. From the Manual (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);

and then in the template:

${product.price?string.@price}
${product.weight?string.@weight}
ddekany
  • 29,656
  • 4
  • 57
  • 64
2

If you just want format use

${amount?string["0.##"]}

or set number_format:

<#setting number_format="0.##">

See all freemarker formats options

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • I probably should've been more specific but I'm basically asking how to use string.currency without the dollar sign. The method you've shown will work but I'm trying to make it simpler for my users in that they don't need to know anything about number formatting and can just type in currency. It's a small difference but internal testing has shown that it's much easier for some people to understand ;) I upvoted you anyways since it's a correct answer and it's my fault for not being more specific. – Stephane Grenier Oct 18 '17 at 06:42
  • See https://stackoverflow.com/questions/8658205/format-currency-without-currency-symbol – Ori Marko Oct 18 '17 at 06:50
  • I was looking at that but it's how to modify an instance of NumberFormat in memory, it doesn't really apply to FreeMarker where you don't have access to the instance from the template file... – Stephane Grenier Oct 18 '17 at 07:21