如何在freemarker中自定义数字格式? [英] How to customize number format in freemarker?

查看:159
本文介绍了如何在freemarker中自定义数字格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用freemarker并试图以这种格式显示数字: $ 3,343,434.00 例如。使用 $ {total?string.currency} (假设total是某个数字)很容易解决这个问题。

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

然而,当我有负数时,它会显示如下:($ 343.34)而不是: - $ 343.34 。我需要负号而不是括号。有没有办法我可以自定义格式,以便它执行 string.currency 所做的所有事情但是替换负值行为?我对freemarker相对较新,所以我们非常感谢您的详细回复!

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!

推荐答案

您还可以尝试?string ( ## 0.00\" )。但是在这种情况下,您需要显式添加 $ - 符号将在 $ <之后/ code>如果是负数。

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

这篇关于如何在freemarker中自定义数字格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆