How to customize number format in freemarker?

前端 未结 5 500
渐次进展
渐次进展 2021-01-13 17:45

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.curre

相关标签:
5条回答
  • 2021-01-13 17:50

    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
    
    0 讨论(0)
  • 2021-01-13 17:51

    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.

    0 讨论(0)
  • 2021-01-13 18:00

    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.

    0 讨论(0)
  • 2021-01-13 18:00

    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

    0 讨论(0)
  • 2021-01-13 18:03

    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.

    0 讨论(0)
提交回复
热议问题