Java 如何在freemarker中自定义数字格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20960069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to customize number format in freemarker?
提问by CustardBun
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).
我正在使用 freemarker 并尝试以这种格式显示数字:$3,343,434.00
例如。这可以通过使用轻松解决${total?string.currency}
(假设“总计”是某个数字)。
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!
但是,当我有负数时,它会像这样显示它们:($343.34)
而不是这样:-$343.34
。我需要负号而不是括号。有没有一种方法可以自定义格式,以便它完成所做的一切string.currency
但替换负值行为?我对 freemarker 比较陌生,所以非常感谢详细的回复!
采纳答案by Mahesh Sapkal
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.
你也可以试试?string(",##0.00")
。但是,在这种情况下,您需要明确添加$
和-
签名$
,以防出现负数。
<#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('$ -','- $')} //- ,343,434.00
回答by Joshua Moore
Freemarker uses the currency formatting provided by the Java platform.
Freemarker 使用 Java 平台提供的货币格式。
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.
它需要对 NumberFormat.getCurrencyInstance() 返回的 DecimalFormat 稍作调整(当您调用 .currency 时会调用它)。您可以在此处查看示例。
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.
但是,这表示在 freemarker 中创建一个宏来调用它可能会更有效,该宏将处理您的特定格式。
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.
很抱歉没有关于该宏的示例,但由于您只是在学习,因此它是 freemarker 中宏的一个很好的入门。
You might investigate if you can supply a custom format using the exposed configuration for number formatsthat will meet your needs.
您可以调查是否可以使用数字格式的公开配置提供自定义格式以满足您的需求。
回答by ddekany
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
更新:从 FreeMarker 2.3.24 开始,您可以定义命名的自定义数字格式,它可以是数字格式模式的别名(甚至是在 Java 中实现的格式化程序,但在这种情况下不需要这种级别的灵活性)。所以添加一个名为“money”的自定义数字格式作为"¤,##0.00"
FreeMarker 配置的别名,然后你可以写一些类似${total?string.@money}
. 请参阅: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
.
目前 FreeMarker 只使用 Java 平台的格式化工具,所以它只能像那样配置(假设你想使用?string
和?string.somethingPredefiendHere
)。这并不多......但是,总的来说,Java平台提供的格式化类别无论如何都不够精细,我的意思是,您没有应用程序域类别,例如产品价格,工资,对股票等价格(这种需求与非货币数字更频繁虽然)。所以我认为,一般来说,你想格式化功能,您可以像使用${salary(someNumber)}
,${price(someNumber)}
等等。这些功能可以在通用#included
/#imported
模板中实现,例如 a#function
或在 Java 中使用#assign salary = 'com.example.SalarayMethod'?new()
代替#function
,其中com.example.SalarayMethod
是 a TemplateMethodModelEx
。
回答by scarecrow
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.
如何取你的号码的 mod,将其转换为所需的字符串格式,最后在最终字符串中添加一个“-”前缀。您只需两步即可保留默认格式。
回答by zavtra
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
提示:?number
在转换为货币格式之前,请确保该数字实际上是具有指令的数字