How to customize number format in freemarker?

freemarker setting number_format
freemarker format number with commas
freemarker format phone number
freemarker format negative number
freemarker date format
freemarker substring
freemarker set currency symbol
freemarker timestamp

I am using freemarker and trying to display numbers in this format: $3,343,434.00 for example. This was easily taken care of by using ${total?string.currency} (assuming "total" is some number).

However, when I have negative numbers, it's showing them like this: ($343.34) instead of this: -$343.34. I need the negative sign instead of the parenthesis. Is there a way I could customize the formatting so it does everything that the string.currency did but replace the negative value behavior? I am relatively new to freemarker, so detailed responses are appreciated!

You can also try ?string(",##0.00"). However in this case you need to explicitly add $ and - sign would be after $ in case of negative numbers.

<#local total = 3343434/>
$ ${total?string(",##0.00")}  //$ 3,343,434.00

<#local total = -3343434/>
$ ${total?string(",##0.00")}  //$ -3,343,434.00

OR in case if you want what was expected you can replace the strings.

<#local total = -3343434/>
<#local total = "$ " + total?string(",##0.00")/>

${total?replace('$ -','- $')}   //- $3,343,434.00

Custom number and date/time formats, The optional format specifier specifies the minimum and the maximum number of displayed fractional digits using syntax mminMmax . For example, m2M5 means " � This built-in converts a number to string for a "computer language" as opposed to for human audience. That is, it formats with the rules that programming languages used to use, which is independent of all the locale and number format settings of FreeMarker.

Update: Since FreeMarker 2.3.24 you can define named custom number formats, which can be an alias to a number format pattern (or even a formatter implemented in Java, but that level of flexibility isn't needed in this case). So add a custom number format called "money" as an alias to "¤,##0.00" to the FreeMarker configuration, and then you can write something like ${total?string.@money}. See: http://freemarker.org/docs/pgui_config_custom_formats.html

Currently FreeMarker just uses the formatting facility of the Java platform, so it's only as configurable as that (assuming you want to use ?string and ?string.somethingPredefiendHere). Which is not much... but, in general, the formatting categories provided by the Java platform is not fine-gradient enough anyway, I mean, you don't have application-domain categories like, price-of-product, a salary, a price on the stock, etc. (This demand is more frequent with non-currency numbers though.) So I think, generally, you want to make a formatter function, that you can use like ${salary(someNumber)}, ${price(someNumber)}, etc. Those functions can be implemented in a commonly #included/#imported template like a #function or in Java by using #assign salary = 'com.example.SalarayMethod'?new() in place of #function, where com.example.SalarayMethod is a TemplateMethodModelEx.

Built-ins for numbers, This built-in exists since FreeMarker 2.3.20. Gives the absolute value of You can also specify a number format explicitly with this built-in, as it will be shown later. There are four is "number" here. You can change this default using a setting:� Hi all, We'd like to use FreeMarker for our project. We have special formatting for different number types (internal id, multiple currencies, quantity) and we'd like to offer our users a comfort of automatic formatting without making them to call build-ins, e.g. use simply

How about taking a mod of your number, convert it to the required string format and finally add a '-' prefix to the final string. You can retain the default format in just two steps.

#{}: Numerical interpolation, You can also specify a number format explicitly with this built-in, as it will be shown later. rather than by FreeMarker, except for computer, which uses the same formatting as the c built-in. You can change this default using a setting:� The optional format specifier specifies the minimum and the maximum number of displayed fractional digits using syntax mminMmax. For example, m2M5 means "at least two, at most five fractional digits".

Freemarker uses the currency formatting provided by the Java platform.

It requires a little tweaking of the DecimalFormat returned by NumberFormat.getCurrencyInstance() (which is what is called when you call .currency). You can see examples of it here.

However, that said it will likely be more effective for you to create a macro in freemarker to call which will handle your specific formatting.

Sorry for not having an example of what that macro would look like, but it's a good starter into macros in freemarker since you are just learning.

You might investigate if you can supply a custom format using the exposed configuration for number formats that will meet your needs.

FreeMarker Manual, 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� Description. Sets the output format to the specified one, inside the nested block. At the end of the block, the earlier output format is restored. This directive only has effect on the section that is literally (as in the text editor) inside the nested bock, not on the parts that are called/included from there.

If you want to maintain the default currency formatting (in case you need to use a locale other than '$'), you can just replace the parentheses like so:

${transaction.amount?string.currency?replace("(","-")?replace(")","")}

This will work without error regardless of if a number is negative or positive.

TIP: Make sure the number is actually a number with the ?number directive before converting to a currency format

Set the template number format, Trigger � Freemarker Examples � File Visibility � Number Currency Format � Error Handling � JSONObject Concatenation How to format numbers and currencies � <#assign tmp=1000000.1246> < #setting locale="fr_FR"> French people write integers like: ${tmp? string ("#,###")} French people write decimals like: ${tmp? string

RunMyProcess User Guide � How to format numbers and currencies, This built-in converts a number to string for a "computer language" as opposed to for human audience. to use, which is independent of all the locale and number format settings of FreeMarker. You can change this default using a setting:� I am new to Freemarker, and can't figure out how to display integers without comma groupings. For instance, I want to display 25000 but instead freemarker displays 25,000. I am seeing lots of things online, such as setting number_format, using the c function, locales, and other options, but none of them seem to work.

Freemarker Default Number Formatting, So there's no way to change the default back to what is sensible. Here's an example of what happens when you render numbers using� I am using Freemarker for a content.ftl file with Geoserver to be displayed in OpenLayers. I am not able to format my numeric values as strings. I can copy and paste the examples from the freemarker FAQ page into my script and they do work. These values are coming from a PostGIS database and are "numeric".

Of course parameter value need not be a string, it can be number, boolean, hash, sequence, etc., also you can use complex expression on the right side of = (e.g. someParam=(price + 50)*1.25). User-defined directives can have multiple parameters. For example, add a new parameter color:

Comments
  • Date and number formatting is better done in #function-s then in #macro-s, unless you formatting is HTML markup (colors and such).