Set the template number format

Background

This article describes how to control the formatting of numbers when they are printed from Freemarker.

Define the default number format for use in a template

The number format setting can be used to define a default format to use when a number is printed from a Freemarker template. This can be set in your .ftl file or an imported macro library.

e.g. Set number to print with commas for the thousands separators, and a full stop for the decimal point. A variable containing the number 1234567.89 will be printed as follows:

<#setting number_format="#,###,###.0">
<#assign examplenumber=1234567.89 />
The example number is ${examplenumber?string}

will print

The example number is 1,234,567.89

Define the number format when a variable is printed

The number format used by a Freemarker template You can control this using the ?string builtin for numbers.

e.g. The following will ensure that the kmFromOrigin value will format the number with commas, and always display at x.x at a minimum (ie. zeros won’t be suppressed for 0.1 or 21.0)

${s.result.kmFromOrigin?string("###,###,##0.0")}

Further reading